Two Dimensional or Array of Arrays in Java

We discuss Two Dimensional or Array of Arrays in Java in this class.

The reader should have prior knowledge of array declaration. Click Here.

Two-dimensional array:

First, We understand two-dimensional arrays.

The below diagram shows an example two-dimensional array.

The two-dimensional array is in tabular form.

The above two-dimensional array has four lines and five columns.

The below diagram shows the coding to declare a two-dimensional array.

class A

{

public static void main()

{

int[][] a = new int[4][5];

a[0][0] = 1;

a[0][1] =2;

System.out.println(a[0][0]);

}

}

The below diagram shows how a two-dimensional array is stored in RAM.

It is an array of arrays.

The first array is of size four. Because four lines in our two-dimensional array.

Take four arrays of size five because each line contains five elements.

Each array address is placed in the first array.

In the above way, the two-dimensional array is stored.

a[0][1] is accesed by looking a[0] has address 5000.

5000 is the address of first line array, so 5000[1] is second element in the array.

The above way is to access the elements in an array.