Isaac
Isaac

Reputation: 321

Java 2D array length

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!

Upvotes: -1

Views: 7495

Answers (5)

geobudex
geobudex

Reputation: 566

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

  }
}

Upvotes: 0

The Scientific Method
The Scientific Method

Reputation: 2436

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

Upvotes: 1

Robbe Vanhaesebroeck
Robbe Vanhaesebroeck

Reputation: 76

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.

Upvotes: 3

campanoon
campanoon

Reputation: 41

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).

Upvotes: 0

Raj
Raj

Reputation: 727

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.

Upvotes: 0

Related Questions