-1

I made a very basic 2D Array that has 3 rows and 4 columns like this.

    int [][]array = new int[3][4];

When I try to check the length like this,

    System.out.println(array.length);

I get a result of 3. However, when I try:

    System.out.println(array[0].length);

I get a result of 4. Same thing using array[1].length, and array[2].length. I'm not sure what the compiler is processing. Can anyone explain?

Thanks!

6
  • 1
    I'm not sure what the compiler is processing.: what does that mean? Are you surprised by this result? Why? Note that there is no such thing as 2D arrays in Java. Only arrays containing arrays. Commented Oct 4, 2018 at 6:41
  • 1
    What did you expect? array.length is the number of rows and array[i].length the number of columns of row i. Commented Oct 4, 2018 at 6:41
  • array is an int[][] (or I should say an array of int[], array[0] is an int[]. Commented Oct 4, 2018 at 6:43
  • stackoverflow.com/questions/7367218/… Commented Oct 4, 2018 at 6:46
  • An array is defined with a fixed length when it's created Commented Oct 4, 2018 at 6:48

5 Answers 5

3

The way you should look at it is that you actually make an array of an array of ints. For example you could initialize it like this:

int[][] array = {
    {1, 2, 3, 4}, // This is the first array in the multidimensional array
    {5, 6, 7, 8}, // This is the second array in the multidimensional array
    {9, 10, 11, 12} // This is the third array in the multidimensional array
};

Note that each of these individual arrays inside the multidimensional array has length 4.

So when you ask the length like this:

array.length // = 3

what Java does is give you the number of int[] in the array. Now every array in this array has length 4, hence:

array[0].length // = 4

Also let's say you wanted to access the element with value 7. This element can be found in the second array on the third place. Since Java indexing starts at 0, you can access that element as follows.

array[1][2] // value of this element is 7

You can read more here https://www.programiz.com/java-programming/multidimensional-array. Multidimensional arrays are hard in the beginning, but once you get them, you will use them often.

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

Comments

1

you can think of the following array variable as array of arrays, it consists of 3 arrays each of which contain 4 elements

int [][]array = new int[3][4];

so when you try array.length you get 3 the first array that contains 3 arrays which contain 4 elements each, however when you array[0].length,array[1].length, array[2].length, you are checking lengths of arrays of elements in array variable, which all give you 4.

Edit:

lets say you want to take out the arrays from array variable array and check lengths

int [] arr1 = array[0]; // arr1.length = array[0].length which is 4
int [] arr2 = array[1]; // arr2.length = array[1].length which is 4
int [] arr3 = array[2];  //arr3.length = array[2].length which is 4

Comments

0

see this is 2D array.

so you have 3 rows and 4 columns so to speak.

That's why when you do array.length it gives number of rows which is 3.

But when you do array[0].length it gives number of columns in that row which is 4.

All rows have same columns that is 4 so you get same result with array[1].length and array[2].length.

2 Comments

Ah, I see I see. Thanks for clarifying!!
Will do! have to wait couple of minutes to do so :)
0

Two dimensional array looks like a matrix and is just an array of array.

If you write:

System.out.println(array[0].length);

Would give you the length of your first array (4) that is the numbers of columns.

If you write:

System.out.println(array.length);

Would give you the length of the numbers of rows (3).

3 Comments

I believe that the matrix comparison is inaccurate.
I know it's not a matrix, but I think it's a good way to imagine how an array of array is.
i just realized that you are actually correct in this case, it's an array afterall.
0

Maybe this example could help demonstrate and answer your question.

public class TwoDArrayLength{

  public static int[][] arrayVar = new int[10][5];
  // returns the length of the rows in the 2d array
  public static int lengthOne = arrayVar.length;
  // returns the length of the columns in the 2d array
  public static int lengthTwo = arrayVar[0].length;
  
  
  public static int lengthThree = arrayVar[1].length; // same value as lengthTwo above

  public static void main(String[] args) {
    System.out.println(lengthOne +"\n"); //10
    System.out.println(lengthTwo+"\n");  //5
    
        System.out.println(lengthThree);//5

  }
}

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.