Get the size of a 2D array

Ok, so I have a 2D z array [50] [50], so the size of z is 50 * 50, but if I say z.length, I only get 50 back. How to get the real size of a 2D array?

+43
java arrays multidimensional-array
Nov 06 '10 at 1:19
source share
2 answers

In Java, 2D arrays are arrays of arrays with possible different lengths (there is no guarantee that in 2D arrays that arrays of the second dimension all have the same length)

You can get the length of any 2-dimensional array as z[n].length where 0 <= n < z.length .

If you treat your 2D array as a matrix, you can simply get z.length and z[0].length , but note that you can make the assumption that for each array in the second dimension the length is the same (for some programs this may be a reasonable assumption).

+77
Nov 06 '10 at 1:22
source share

Turning around what Mark Elliot said earlier, the easiest way to get the size of a 2D array, given that each array of an array of arrays has the same size, is:

 array.length * array[0].length 
+7
Mar 19 2018-11-11T00:
source share



All Articles