views:

39

answers:

1

Would anyone please explain the output showm at the bottom for this code. I am somewhat confused about this part and understanding what is stored in doc after execution...
doc[a][b] = a + b;

public class doc  
{  
    public static void main(String[] args)  
    {  
        int b = 0;  
        int [][] doc = new int [3][3];  
        int a;  
        while (b<3)  
        {  
            for(a=2; a >=0; a--)  
            doc[a][b] = a + b;  
            ++b;  
        }  
        int j;  
        for (int i=0; i<doc.length; i++) {  
            for (j=0; j<doc[i].length; j++) {  
                System.out.println(" " + doc[i][j]);            }  
            System.out.println("");  
        }  


    }  
}  

0
1
2

1
2
3

2
3
4

The output is above.
Thank you.

+1  A: 

The array will look like the following

0 1 2
1 2 3
2 3 4

All it is doing is going through and printing out each row in succession. In the for loop

for (int i=0; i<doc.length; i++) {  
        for (j=0; j<doc[i].length; j++) {  
            System.out.println(" " + doc[i][j]);            }  
        System.out.println("");  
    }  

i represents the row number and j represents the column number so it says go to row 0 and print out column 1, the col 2, then col 3. Now go to row 1 and print col 1, col 2 and col 3 and so forth with row 2.

Mike