Multidimensional Arrays lengths in Java

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

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

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;

Leave a Comment