# Thread: Please explain following Program - Two dimensional array

1. Member
Join Date
Jan 2013
Posts
2
Rep Power
0

## 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

2. ## 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.

3. Member
Join Date
Jan 2013
Posts
2
Rep Power
0

## 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

4. ## Re: Please explain following Program - Two dimensional array

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•