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)
We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.
The representation of the elements is in rows and columns. Thus, you can get a total number of elements in a multidimensional array by multiplying row size with column size. So if you have a two-dimensional array of 3×4, then the total number of elements in this array = 3×4 = 12.
Try using array[0]. length , this will give the dimension you're looking for (since your array is not jagged).
double average1 = 0; double average2 = 0; We find the sum of the elements of each array and then divide each sum by the number of elements in each array. We do this to find the average of each array.
Try using array[0].length
, this will give the dimension you're looking for (since your array is not jagged).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With