Finding the length of all arrays of a multidimensional array, Java

I would like to use a multidimensional array to store a data grid. However, I did not find an easy way to find the length of the 2nd part of the array. For example:

boolean[][] array = new boolean[3][5]; System.out.println(array.length); 

outputs only 3 .

Is there another simple command to find the length of the second array? (i.e. exit 5 in the same way)

+7
source share
5 answers

Try using array[0].length , this will give the dimension you are looking for (since your array is not jagged).

+8
source
 boolean[][] array = new boolean[3][5]; 

Creates an array of three arrays (5 boolean each). In Java, multidimensional arrays are just arrays of arrays:

 array.length 

gives the length of the "external" array (in this case 3).

 array[0].length 

gives the length of the first "internal" array (in this case 5).

 array[1].length 

and

 array[2].length 

will also give you 5, since in this case all three "internal" arrays array[0] , array[1] and array[2] are the same length.

+7
source

array [0] .length will give you 5

+1
source
 int a = array.length; if (a > 0) { int b = array[a - 1].length; } 

should do the trick, in your case a will be 3, b 5

+1
source

You want to get the length of the internal array in a three-dimensional array

eg.

 int ia[][][] = new ia [4][3][5]; System.out.print(ia.length);//prints 4 System.out.print(ia[0].length);//prints 3 System.out.print(ia[0].[0].length); // prints 5 the inner array in a three D array 

By induction: for a four-dimensional array, this is:

 ia[0].[0].[0].length 

......

0
source

All Articles