获取二维数组的大小

发布于 2024-10-01 01:51:31 字数 87 浏览 1 评论 0原文

好的,我有一个 2D 数组 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2024-10-08 01:51:31

在 Java 中,二维数组实际上是长度可能不同的数组的数组(不能保证在二维数组中,二维数组的长度都相同)。

您可以通过 z[ 来获取任何二维数组的长度n].length 其中 0 <= n z.长度

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

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)

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

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

Bonjour°[大白 2024-10-08 01:51:31

扩展 Mark Elliot 之前所说的内容,考虑到数组中的每个数组,获取 2D 数组大小的最简单方法具有相同大小的数组为:

array.length * array[0].length

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:

array.length * array[0].length
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文