Getting the length of two-dimensional array

JavaArrays

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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]);

Solution 5 - Java

int secondDimensionSize = nir[0].length;

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

Solution 6 - Java

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

Solution 7 - Java

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.

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

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.

Solution 11 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNumeratorView Question on Stackoverflow
Solution 1 - JavaBrian RoachView Answer on Stackoverflow
Solution 2 - JavaRasmus NorupView Answer on Stackoverflow
Solution 3 - JavakrtekView Answer on Stackoverflow
Solution 4 - JavaconfuciusView Answer on Stackoverflow
Solution 5 - JavadlevView Answer on Stackoverflow
Solution 6 - JavaIgor SoudakevitchView Answer on Stackoverflow
Solution 7 - JavaMartijn CourteauxView Answer on Stackoverflow
Solution 8 - JavaPiyush MattooView Answer on Stackoverflow
Solution 9 - JavaShambhaviView Answer on Stackoverflow
Solution 10 - Javauser3504090View Answer on Stackoverflow
Solution 11 - JavaMcBlankenburgView Answer on Stackoverflow