多维数组长度在Java中 [英] Multidimensional Arrays lengths in Java

查看:116
本文介绍了多维数组长度在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到与非平等指数多维数组的长度。

I need to find the lengths of a multidimensional array with non equal indices.

例如,我是int [] [] = pathList新INT [6] [4]

For example, I have int[][] pathList = new int[6][4]

实际上没有硬编码的指数,我需要找到'6'和'4'。

Without actually hard-coding the indices, I need to find the '6' and the '4'.

我可以找到6 pathList.length但我将如何获得'4'?

I can find the 6 with pathList.length but how would I obtain the '4'?

推荐答案

这将使你在索引数组长度 I

This will give you the length of the array at index i

pathList[i].length

要注意的是不同于C或C ++,二维阵列中的Java元件的长度不必相等是很重要的。例如,当 pathList 被实例化等于新INT [6] [] ,它可以容纳6 INT [] 实例,每个实例都可以有不同的长度。

It's important to note that unlike C or C++, the length of the elements of a two-dimensional array in Java need not be equal. For example, when pathList is instantiated equal to new int[6][], it can hold 6 int [] instances, each of which can be a different length.

所以,当你创建数组,你在你的问题已经证明了,你不妨做

So when you create arrays the way you've shown in your question, you may as well do

 pathList[0].length

因为你知道,他们都具有相同的长度。在其他情况下,您需要定义,具体到你的应用程序完全相同的什么的第二个维度的长度是指 - 这可能是最大长度的所有元素,或者是最低的。在大多数情况下,你需要遍历所有的元素和阅读它们的长度做出决定:

since you know that they all have the same length. In the other cases, you need to define, specific to your application exactly what the length of the second dimension means - it might be the maximum of the lengths all the elements, or perhaps the minimum. In most cases, you'll need to iterate over all elements and read their lengths to make a decision:

for(int i = 0; i < pathList.length; i++)
{
    int currLen = pathList[i].length;
}

这篇关于多维数组长度在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆