Nayrb
Nayrb

Reputation: 1043

Multidimensional Arrays lengths in Java

How to find the lengths of a multidimensional array with non equal indices?

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

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

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

Upvotes: 31

Views: 123802

Answers (9)

Martin Boeck
Martin Boeck

Reputation: 21

pathList.length gives you the number of rows. This means it will output 6 for int[6][4] pathList[i].length gives you the number of columns in the ith row. Since int[6][4] can be seen as a rectangle it will always output 4.

Upvotes: 2

Anky
Anky

Reputation: 1

You can find '4' by using pathlist[i].length

Please rectify me if I am wrong as I am a novice and would help me in understanding Java better. The following code may help you in understanding.

public class Main {

    public static void main(String[] args) {


        int num[][] = new int[1][3];
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < num[i].length; j++) {
                num[i][j] = 10;
                System.out.println("num [" + i + "] [" + j + "] = " + num[i][j]);
            }
        }
    }
}

Upvotes: 0

Manouchehr Khatami
Manouchehr Khatami

Reputation: 1

In Java we can't use Length field like we used to one-dimensional arrays. So simply writing a few lines of code solves this problem. First, you need to know that the output of the Length field in multidimensional arrays is the number of rows.I mean when you have below array

int[][] numbers = {{1,2,3,4,2,6},{4,5,6,7}};

the result of System.out.println(numbers.length); is 2, because you have 2 rows. So, you should use this to solve this problem. Example:

public class Main {

public static void main(String[] args) {

    //Array definition
    int[][] numbers = {{1,2,3,4,2,6},{4,5,6,7}};

    //Number of array's elements
    int result = 0;

    //calculate via loop
    for(int i=0; i< numbers.length; i++){
        result +=  numbers[i].length;
    }

    //output
    System.out.println(result);
}

}

Upvotes: 0

KSRKSR
KSRKSR

Reputation: 139

3-D array length

int[][][] a = new int[2][3][7];
int length=0;
for(int[][] i:a){
    for(int j[]:i){
        length+=j.length;
    }   
}           
System.out.println(length);

Upvotes: -1

Vaibhav Grover
Vaibhav Grover

Reputation: 319

In java we can define array of arrays which we call multi dimensional arrays.By array of arrays we mean that a single elment of our array is again an array (in java can be of multiple length).To find length of multi array having all subarray of same size,we can use:

int[][]a=new int[3][3];//let a[][] be my array
a.length will work.   //a is an object of proxy class and length is its property.

However,if you have subarrays of different sizes then you have to iterate it.

for(i=0;i<a.length;i++)
        int cur_size=a[i].length;

Upvotes: 1

Nikhil Kumar
Nikhil Kumar

Reputation: 2924

For 2 D array :-

int x[][] = new int[6][12];
System.out.println(x.length + " " + x[1].length);

OUTPUT : 6 12

Upvotes: 4

Siddharth Thevaril
Siddharth Thevaril

Reputation: 3798

This is for a 3 dimensional array.

 int x[][][]=new int[5][8][10];
        System.out.println(x.length+" "+x[1].length+" "+x[0][1].length);

OUTPUT : 5 8 10

Upvotes: 13

no.good.at.coding
no.good.at.coding

Reputation: 20371

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

pathList[i].length

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;
}

Upvotes: 67

Nathan Ryan
Nathan Ryan

Reputation: 13061

Java has "jagged" multidimensional arrays, which means that each "row" in your two-dimensional array can have a different number of components. If you can assume that each row has the same number of components, use:

pathList[0].length;

Otherwise, you will have to iterate:

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

Upvotes: 7

Related Questions