获取二维数组的大小

获取二维数组的大小

本文介绍了获取二维数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个二维数组 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)

你可以得到任何二维数组的长度为 z[n].length 其中 0 z.length.

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

如果您将二维数组视为矩阵,则可以简单地获取 z.lengthz[0].length,但请注意,您可能假设对于第 2 维中的每个数组,长度是相同的(对于某些程序,这可能是一个合理的假设).

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

这篇关于获取二维数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:22