0

I am new to the concept of arrays in java, and as part of an assignment, I am required to create a 10 row by 3 column integer array and populate it with a for loop. I have the following.

  public class arrays{

  int arr[][][] = new int [10][10][10];

    for(int i=0;i<arr.length;++i)
    {
        for(int j=0;j<arr[i].length;++j)
        {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }
} 

am I on the right track?

3
  • If your idea of array dimension syntax was actually used by Java, how do you imagine creating a 10 row by 3,000,000 column array would look like? Commented Mar 3, 2014 at 10:38
  • I dunno. hence am here seeking help., Commented Mar 3, 2014 at 10:43
  • @user3263215 I hope it is now clear for you, see the trails tutorial as well. Commented Mar 3, 2014 at 10:45

2 Answers 2

1

You are actually declaring a 3-dimensional array.
You should instead be declaring int[][] arr = new int[10][3];.
You can then assign values as follows: arr[i][j] = 42;

Note also that when instantiated, the initial values in the array are 0.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah i see. I am trying to populate the first and second column with some numbers. Is the for loop i've got suitable for this?
@user3263215 looks fine for iterating through.
0

The way of creating array in java is simple and I see you know that but for your problem applicable array might be different. Try that one int arr[][] = new int [10][3];

for(int i=0;i<arr.length;i++){
  for(int j=0;j<arr[i].length;j++){
      arr[i][j] = your_integer_value;
   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.