[java] Getting the length of two-dimensional array

How do I get the second dimension of an array if I don't know it? array.length gives only the first dimension.

For example, in

public class B {
    public static void main(String [] main){
        int [] [] nir = new int [2] [3];
        System.out.println(nir.length);
    }
}

how would I get the value of the second dimension of nir, which is 3.

Thanks

This question is related to java arrays

The answer is


which 3?

You've created a multi-dimentional array. nir is an array of int arrays; you've got two arrays of length three.

System.out.println(nir[0].length); 

would give you the length of your first array.

Also worth noting is that you don't have to initialize a multi-dimensional array as you did, which means all the arrays don't have to be the same length (or exist at all).

int nir[][] = new int[5][];
nir[0] = new int[5];
nir[1] = new int[3];
System.out.println(nir[0].length); // 5
System.out.println(nir[1].length); // 3
System.out.println(nir[2].length); // Null pointer exception

Here's a complete solution to how to enumerate elements in a jagged two-dimensional array (with 3 rows and 3 to 5 columns):

    String row = "";
    int[][] myArray = {{11, 12, 13}, {14, 15, 16, 17}, {18, 19, 20, 21, 22}};
    for (int i=0; i<myArray.length; i++) {
        row+="\n";
        for (int j = 0; j<myArray[i].length; j++) {
            row += myArray[i][j] + "  ";
        }
    }
    JOptionPane.showMessageDialog(null, "myArray contains:" + row);

Jagged array's contents


Expansion for multi-dimension array total length,

Generally for your case, since the shape of the 2D array is "squared".

int length = nir.length * nir[0].length;

However, for 2D array, each row may not have the exact same number of elements. Therefore we need to traverse through each row, add number of elements up.

int length = 0;
for ( int lvl = 0; lvl < _levels.length; lvl++ )
{
    length += _levels[ lvl ].length;
}

If N-D array, which means we need N-1 for loop to get each row's size.


Remember, 2D array is not a 2D array in real sense.Every element of an array in itself is an array, not necessarily of the same size. so, nir[0].length may or may not be equal to nir[1].length or nir[2]length.

Hope that helps..:)


Assuming that the length is same for each array in the second dimension, you can use

public class B {    
 public static void main(String [] main){
    int [] [] nir= new int [2] [3];
    System.out.println(nir[0].length);
 }
}

nir[0].length

Note 0: You have to have minimum one array in your array.
Note 1: Not all sub-arrays are not necessary the same length.


You can do :

 System.out.println(nir[0].length);

But be aware that there's no real two-dimensional array in Java. Each "first level" array contains another array. Each of these arrays can be of different sizes. nir[0].length isn't necessarily the same size as nir[1].length.


//initializing few values
int[][] tab = new int[][]{
    {1,0,1,0,1,0,1,0},
    {0,1,0,1,0,1,0,1},
    {1,0,1,0,1,0,1,0},
    {0,1,0,1,0,1,0,1},
    {1,0,1,0,1,0,1,0},
    {0,1,0,1,0,1,0,1},
    {1,0,1,0,1,0,1,0},
    {0,1,0,1,0,1,0,1}
};

//tab.length in first loop
for (int row = 0; row < tab.length; row++)
{
    //tab[0].length in second loop
    for (int column = 0; column < tab[0].length; column++)
    {
        //printing one value from array with space
        System.out.print(tab[row][column]+ "   ");
    }
    System.out.println(); // new row = new enter
}

int secondDimensionSize = nir[0].length;

Each element of the first dimension is actually another array with the length of the second dimension.


In the latest version of JAVA this is how you do it:

nir.length //is the first dimension
nir[0].length //is the second dimension

use

   System.out.print( nir[0].length);

look at this for loop which print the content of the 2 dimension array the second loop iterate over the column in each row

for(int row =0 ; row < ntr.length; ++row)
 for(int column =0; column<ntr[row].length;++column)
    System.out.print(ntr[row][column]);