Finding length of all arrays multidimensional array, Java
Solution 1
Try using array[0].length
, this will give the dimension you're looking for (since your array is not jagged).
Solution 2
boolean[][] array = new boolean[3][5];
Creates an array of three arrays (5 boolean
s each). In Java multidimensional arrays are just arrays of arrays:
array.length
gives you the length of the "outer" array (3 in this case).
array[0].length
gives you the length of the first "inner" array (5 in this case).
array[1].length
and
array[2].length
will also give you 5, since in this case, all three "inner" arrays, array[0]
, array[1]
, and array[2]
are all the same length.
Solution 3
array[0].length would give you 5
Solution 4
int a = array.length;
if (a > 0) {
int b = array[a - 1].length;
}
should do the trick, in your case a would be 3, b 5
HedonicHedgehog
Updated on November 13, 2020Comments
-
HedonicHedgehog over 3 years
I would like to use a multidimensional array to store a grid of data. However, I have not found a simple 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);
will only output 3.
Is there another simple command to find the length of the second array? (i.e. output 5 in the same manner)