Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class TwoDArray { 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(); }
}
}

What I have tried:

I understood what output i am getting but i don't know how i am getting it. please let me know what is happening inside for loop.
Posted
Updated 18-Nov-18 18:33pm

Firstly, here's the code in a bit more "readable" format, which makes it a bit easier to interpret:
Java
class TwoDArray {
 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();
  }
 }
}
You have four for loops totally (two outer for loops with each an inner for loop).
Java
for (i = 0; i < 4; i++)
 for (j = 0; j < 5; j++) {
  twoD[i][j] = k;
  k++;
 }
The above is the first outer loop. The code that's ran in the outer loop is not wrapped within { } so that means that only the next statement will be ran in the loop, and here this statement is another loop! So you have a nested loop[^], a loop in a loop. When your loop runs, this is what will happen with the variables i, j and k (before k++) in chronological order:
  • i = 0, j = 0, k = 0
  • i = 0, j = 1, k = 1
  • i = 0, j = 2, k = 2
  • i = 0, j = 3, k = 3
  • i = 0, j = 4, k = 4
  • i = 1, j = 0, k = 5
  • i = 1, j = 1, k = 6
  • i = 1, j = 2, k = 7
  • i = 1, j = 3, k = 8
  • i = 1, j = 4, k = 9
  • i = 2, j = 0, k = 10
  • ... and so on. As you can see, for each i, the whole loop for j runs.


Then, the second outer loop:
Java
for (i = 0; i < 4; i++) {
 for (j = 0; j < 5; j++)
  System.out.print(twoD[i][j] + " ");
 System.out.println();
}
Same idea: it's a nested loop. But here, the inner loop doesn't have brackets to specify the block for the loop 'contents', so only the next statement belongs to the loop, which is the print statement - the println only belongs to the outer loop, and not to the inner loop.
 
Share this answer
 
Comments
Anil Pillai 28-Jan-18 5:01am    
Thanks bro
Start by indenting your code so it's readable:
Java
class TwoDArray { 
    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(); 
            }
        }
    }
Now you can see what is going on: you have two nested loops, one which which indexes i through all rows, and an inner one which indexes j through all columns.
Inside the inner loop, you set each cell in the array to a number from an incrementing sequence. Because the inner loop works on all the columns in a row, each column gets a value one greater than its predecessor, and then you move to the next row and continue with it's columns.
 
Share this answer
 
Comments
Anil Pillai 28-Jan-18 2:36am    
ok i understood but can you tell me how k is working there? what exactly this code is doing'twod[i][j]=k' ?
OriginalGriff 28-Jan-18 4:47am    
It's an assignment. The left hand side of the equals sign is the destination, the right hand side is the source. The value in the source is copied to the destination.
In this case, the destination is an array element: your array is called twoD and it requires two indexes: a "row index" and a "column index", in the same way that a piece of graph paper needs an X and a Y coordinate in order to specify a particular "box" on the sheet. "i" and "j" provide those indexes.

To be honest, if you don't understand this code you need to go right back to the start of your course, and re-read all your notes - this is very basic stuff!
Anil Pillai 28-Jan-18 5:02am    
Thank you
When you don't understand that some code is doing, use the debugger, it will show you.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Quote:
what exactly this code is doing'twod[i][j]=k' ?

Like for any language, you need to learn the basics.
 
Share this answer
 
v2
Comments
Anil Pillai 28-Jan-18 5:02am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900