0

I misunderstand something in this for loop. Can someone please clarify?

String[][] artikelTabelle;

    artikelTabelle = new String[2][2]; 

    artikelTabelle[0][0] = "Cow";
    artikelTabelle[0][1] = "Sheep";
    artikelTabelle[1][0] = "Dog";
    artikelTabelle[1][1] = "Lion";


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

At first i and j are 0. Consequently, the array [0][0] (Cow) is printed. But during the second iteration, aren't i and j both 1, because of i++ and j++? Meaning that Lion should be printed? What do I misunderstand here? And since artikelTabelle[0].length has the length 2 - why would it be false to replace it with the number 2?

1
  • be vary that the inner loop ( the one with j ) is iterated twice, before i is incremented. Commented Jul 26, 2017 at 11:38

4 Answers 4

5

You have two loops, one outer loop and another inner loop nested inside the outer loop.

This means that for each iteration of the outer loop (before each time i is incremented), the entire inner loop is executed (i.e. j goes from 0 to artikelTabelle[0].length before i is incremented).

Therefore first artikelTabelle[0][0] is printed, followed by artikelTabelle[0][1] (not by artikelTabelle[1][1]). artikelTabelle[1][1] is only printed in the second iteration of the outer loop.

As for whether to use 2 instead of artikelTabelle[0].length, the latter is safer, since you don't have to change a hard-coded 2 if later you change the dimensions of the array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I think I understand now. But to be sure: At first the inner loop is executed, before it gets incremented. Then artikelTabelle[0][0] is printed. And then the inner loop is executed again with j incremented. j is one and artikelTabelle[0][1] gets printed. Then the inner for loop notices, that if it increments again (to 2) the condition that 2 < 2 won't work. Therefore it goes to the outer loop and increments i. i is now 1. It doesn't go through the inner for loop and prints artikelTabelle[1][0]. Then the inner for loop is executed again and artikelTabelle[1][1] is printed. Is that right?
@TeachMeSenpai Yes that's right, assuming you meant to write It goes through and not It doesn't go through in the next to last sentence.
2

artikelTabelle[0].length is indeed 2.

However, you are misreading the loops. It goes like this:

You start off with i being 0, and j being 0. You print out artikelTabelle[0][0]. Now, we're still in the second for loop. Our condition for termination hasn't been met yet. So i is still 0, and we increment j to 1. Now we print out artikelTabelle[0][1]. Since our condition is now met to terminate the second for loop, we resume our first loop. i is incremented to 1, and we repeat the process. We print out artikelTabelle[1][0] and then artikelTabelle[1][1].

Hopefully I was able to help!

Comments

2

The outer for loop will execute whatever is inside it for the amount of times you specified.

The inner code (which is also a for loop here) will thus be executed multiple times.

The variable j is scoped to, and initialized in the outer loop, meaning that when the outer loop starts for the second time, the previous variable no longer exists.

Comments

1

in you code artikelTabelle[0].length = 2 and artikelTabelle.length = 2so for outer i loop increased by 1 than inner j for loop loopind two time like this :

i =0 =>j=0,j=1 ; i =1 =>j=0,j=1 ;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.