Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding length of all arrays multidimensional array, Java

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)

like image 464
HedonicHedgehog Avatar asked Sep 19 '12 21:09

HedonicHedgehog


People also ask

How do you find the length of a multidimensional array?

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.

What is the length of a multidimensional array in Java?

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.

How do you find the length of an array of arrays?

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

How does multidimensional array calculate average in Java?

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.


1 Answers

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

like image 52
arshajii Avatar answered Sep 28 '22 01:09

arshajii