Length is: 3
The length of a 2D array is the number of rows it has. The row index runs from 0 to length-1.
Each row of a 2D array can have a different number of elements,
so each row has its own length
:
class unevenExample3 { public static void main( String[] arg ) { // declare and construct a 2D array int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; // length of the array (number of rows) System.out.println("Length of array is: " + uneven.length ); // length of each row (number of its columns) System.out.println("Length of row[0] is: " + uneven[0].length ); System.out.println("Length of row[1] is: " + uneven[1].length ); System.out.println("Length of row[2] is: " + uneven[2].length ); } }
Notice that uneven[0]
refers to row 0 of the array,
uneven[1]
refers to row 1, and so on.