获取二维数组的大小 [英] Get the size of a 2D array
问题描述
好了,所以我有一个二维数组z [50] [50]和z的尺寸因此50 * 50,但如果我说z.length我只得到50回。我如何获得一个二维数组的实际大小?
Okay, so I have a 2D array z[50][50] and z's size is therefore 50 * 50, but if I say z.length I only get 50 back. How do I get the real size of a 2D array?
推荐答案
在Java中,二维数组是真正的数组的数组可能具有不同长度(有没有保证,在二维数组的第二维阵列都具有相同的长度)
In Java, 2D arrays are really arrays of arrays with possibly different lengths (there are no guarantees that in 2D arrays that the 2nd dimension arrays all be the same length)
您可以得到任何第2维数组的长度 Z [N]。长度
,其中 0℃; = N< z.length
。
You can get the length of any 2nd dimension array as z[n].length
where 0 <= n < z.length
.
如果你对待你的二维数组作为基质,你可以简单地获得 z.length
和 Z [0]。长度
,但请注意,你可能会做一个假设,即在第二维每个数组长度是一样的(对于某些程序,这可能是一个合理的假设)。
If you're treating your 2D array as a matrix, you can simply get z.length
and z[0].length
, but note that you might be making an assumption that for each array in the 2nd dimension that the length is the same (for some programs this might be a reasonable assumption).
这篇关于获取二维数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!