获取2D数组的大小[复制]

时间:2022-01-22 21:28:32

This question already has an answer here:

这个问题已经有了答案:

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?

好的,我有一个二维数组z[50][50], z的尺寸是50 * 50,但是如果我说z。我只拿回50。如何得到一个二维数组的实际大小?

2 个解决方案

#1


77  

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)

在Java中,二维数组实际上是数组的数组,其长度可能不同(在二维数组中,没有保证第2维数组的长度相同)

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

你可以得到任何二维数组的长度为z[n]。长度为0 <= n < z.长度。

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).

如果你把二维数组看成矩阵,你可以得到z。长度和z[0]。长度,但请注意,您可能会假设,对于第二个维度中的每个数组,长度是相同的(对于某些程序,这可能是一个合理的假设)。

#2


8  

Expanding on what Mark Elliot said earlier, the easiest way to get the size of a 2D array given that each array in the array of arrays is of the same size is:

扩展到Mark Elliot之前说过的,最简单的方法是得到一个二维数组的大小,因为数组数组中的每个数组的大小都是相同的:

array.length * array[0].length

#1


77  

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)

在Java中,二维数组实际上是数组的数组,其长度可能不同(在二维数组中,没有保证第2维数组的长度相同)

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

你可以得到任何二维数组的长度为z[n]。长度为0 <= n < z.长度。

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).

如果你把二维数组看成矩阵,你可以得到z。长度和z[0]。长度,但请注意,您可能会假设,对于第二个维度中的每个数组,长度是相同的(对于某些程序,这可能是一个合理的假设)。

#2


8  

Expanding on what Mark Elliot said earlier, the easiest way to get the size of a 2D array given that each array in the array of arrays is of the same size is:

扩展到Mark Elliot之前说过的,最简单的方法是得到一个二维数组的大小,因为数组数组中的每个数组的大小都是相同的:

array.length * array[0].length