# Please explain following Program - Two dimensional array

• 01-10-2013, 08:19 PM
learner09
Please explain following Program - Two dimensional array
Hi Friends , I am not getting logic of this program and amused by output , Can anyone explain it clearly step by step -

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

for(i=0; i<4; i++)
for(j=0; j<5; j++) { twoD[i][j] = k; k++;

}

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

Output

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
• 01-10-2013, 09:13 PM
sehudson
Re: Please explain following Program - Two dimensional array
Are you familiar with how a loop works? If not, it may be easier to look at a tutorial on loops first, before trying to understand a nested loop, which is what this program involves (a loop inside of a loop). Also, you can take advantage of the System.out.println() function to print out the values of the twoD[][] as its populated as the program runs, that should help you too.
• 01-11-2013, 06:56 AM
learner09
Re: Please explain following Program - Two dimensional array
Please explain step by step , I know loops but not able to understand how this works .Would be a good help
• 01-11-2013, 07:52 AM
DarrylBurke
Re: Please explain following Program - Two dimensional array