Help with a two dimension array, how does it produce this output?

This is the class:

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();

}

}

}

This generates

0 1 2 3 4

5 6 7 8 9

10 11 12 13 14

15 16 17 18 19

How?

I have a few questions:

What is k needed for? Where is it useful in the output?

Why does the first for block start at the end of j when the second starts at the end of i?

If K is used to create the output 0-19, how is it used in theprintln method? Isn’t it nested? Or is the for loop for J now associated with the values of K, and if I is acting on J, it’s using the nested values of J that the public can’t see?

Thanks,

Christian

First, if you would format your code as code by clicking the </> icon, and then paste your code in the space indicated, that will make your code much easier to read. I had to add some curly braces, but your formatted code would look like this:

  public 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();
      
      }

    }

  }

The integer variable k is initialized at 0 and incremented after each assignment to your array. It is where the values are coming from that make up the elements of the array. By nesting for loops, you are able to iterate through the 2 dimensional array, and populate each index with the value of k. Let’s look at it step by step:

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

First iteration:
i = 0 and j = 0 and k = 0, so twoD[0][0] = 0
Second iteration:
i = 0, j = 1 and k = 1, so twoD[0][1] = 1
Third iteration:
i = 0, j = 2 and k = 2, so twoD[0][2] = 2
Fourth iteration:
i = 0, j = 3 and k = 3, so twoD[0][3] = 3
Fifth iteration:
i = 0, j = 4 and k = 4, so twoD[0][4] = 4
Now j will be 5, so the inner for loop is finished, and i gets incremented to 1:
Sixth iteration:
i = 1, j = 0 and k = 5, so twoD[1][0] = 5
This process continues until the entire array is populated.
Fast Forward…
Twentieth iteration:
i = 3, j = 4 and k = 19, so twoD[3][4] = 19

When the array is printed, k is no longer needed. The array already has the values assigned to it. You just iterate through the indexed addresses using the nested for loops again, and print each value. It may help to visualize this array as a grid like so:


Hope this helps!

1 Like

Thank you so much. It helped immensely.
In here:

for (i=0; i<4; i++) { 

        for(j=0; j<5; j++) {

          System.out.print(twoD [i] [j] + " ");
          
        }

        System.out.println();
      
      }

What’s the first { for at the start of the for i loop?

Also,

{

          twoD [i] [j] = k;

          k++;

        }

is within a block of code, doesn’t that mean it shouldn’t be able to be used outside of the block? Like how does the rest of the class know that the array has values assigned to it if it was assigned in a block of code?

Thanks,

Christian

See my comments in the code above for a description of the curly braces.

To answer your second question, the 2 dimensional array was declared outside of the code block you’re referring to with this line: int twoD[][] = new int [4][5]; Its declaration occurs just inside the main function block, so it can be accessed and manipulated anywhere inside the main function.

1 Like

Thank youuuuu!!!

1 Like