How to get the length of the 2D array in Java

Two dimensional array in java

To declare an 2D array in java we use the following syntax

Datatype [][] arrayName=new Datatype[rows][cols]

If you want to declare any primitive 2D array.

int [][] ages=new int[3][4];

double [][] prices=new double[4][4]

Array ages is a two-dimensional int array, it can store 3 rows and each row can contain 4 values (columns) total of 3*4=12 values.

similar price is a two-dimensional double array, it can store 4 rows and 4 columns total (4*4) 20 elements.

Two-dimensional array initialization

above are the two-dimension array declared and initialized.

Length of an array in Java

Java array provides a variable length to find the length of a variable.4

to find the length of one dimension array we use following code

One dimension array length is number of elements in array

Length of the 2D array in Java

In a two-dimensional array, java length is the number of rows in 2D array.

To access each row length we can use the following code