Two-Dimensional Arrays in Java

In Java, multi dimensional arrays are also referred to as arrays of arrays. 2D array is the commonly used and simplest multi-dimensional array. Generally ,2D arrays are referred to as one-dimensional arrays lists. 2D arrays are represented in a row-columns form on paper, and the terms "rows" and  "columns" are used in computing.

 

class twodim
{
public static void main(String ana[])
{
int aa,bb;
int table[][]=new int[4][5];
for(aa=0;aa<4;++aa)
{
for(bb=0;bb<5;++bb)
{
table[aa][bb]=(aa*5)+bb+1;
System.out.print(table[aa][bb]+"   ");
}
System.out.println();
}
}
}

Two dimensional array in java

Explanation....

In this example a class twodim is created. In main method, two integer variables aa,bb are declared,which are used for the two dimensional array.Then, a two dimensional array is created new int[4][5], which is linked to a reference variable table of the array.After this, first for loop is used, wherein variable aa is initialized to 0, and aa condition aa<4 is checked.
If the condition aa<4 is true ,second for loop starts.
In this for loop
first, the value of bb is initialized to 0
Then, the condition bb<5 is checked.
If the condition bb<5 is true, table[aa][bb] is assigned a value that comes as a result of (aa*5)+bb+1;
Then the System.out.print(table[aa][bb]+" "); statement is used to print the value that is assigned to table[aa][bb].

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @