Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

can any one explain Two -Dimensional Arrays (code)

Sekhar Velagala
Greenhorn
Posts: 6
Can anyone explain this code...what is the use of firset of for loops and second set of for loops:

Class array{
public static void main(String args[]){
int a[][] = new int [4][5];
int i,j, k=0;

for(i=0;i<4;i++)
for(j=0;j<n;j++){
a[i][j]=k;
k++;
}

for(i=0;i<4;i++){
for(j=0;j<n;j++)
System.out.print(a[i][j] + "");
System.out.println();
}
}
}

Thanks,
Sekhar

Deepak Bala
Bartender
Posts: 6663
5
what exactly is the variable n doing here ? this code will not compile. variable n is not declared.

Sekhar Velagala
Greenhorn
Posts: 6
Hello John,

I am sorry...i mistyped the character...it is 5 instead of n in for loop...

Thanks,
Sekhar

Arno Reper
Ranch Hand
Posts: 286
hi,
an array of array is what we call in french "a matrice" , you have two for-loop because you have to move trough the rows and columns like :

its like chess or every GridGame
i hope this help,
arno

Naresh Gunda
Ranch Hand
Posts: 163
a[4][5] -> Array 'A' contains 4 rows , 5 columns. By default Array index starts with zero.

[ April 09, 2006: Message edited by: Naresh Kumar ]

Arno Reper
Ranch Hand
Posts: 286
ups it wasn't a[5][4],
my apologizes...
arno

Swosti Dipan Pal
Ranch Hand
Posts: 70
The main purpose of the first set of loop is to intialize the value of the array elements with the value of k.

The second set is used to display the values of the array element.

Cheers,
-Biswa