11

I'm currently learning Java online and am confused about the following code and what one of the elements in the array is evaluating to:

int[] a = new int[]{9, 8, 3, 1, 5, 4};

for (int i = 0; i < a.length; i++) {
    if (a[i] % 2 == 0) {
        a[i] += 1;
    } else if (a[i] < a.length) {
        a[i] += a[a[i]];
    }
}

I am looking at a[3] and the number that this evaluates to, and when I am debugging the code, my IDE is showing that a[a[i]] is evaluating to 9, which is where I'm a bit confused.

I thought that a[3] would equal 1 and then a[1] would equal 8, however this doesn't seem to be the case. Could anyone provide clarity as the JetBrains Academy course doesn't refer to this.

2
  • The best way to see this is to simply get out pen and paper and follow thru the logic. Just remember that sometimes, future indices point to values that have been altered. So always reference the current state of the array. Commented Sep 17, 2019 at 13:01
  • Thanks for the tip, I do enjoy working on paper so will try this in future when stuck. Commented Sep 18, 2019 at 18:01

3 Answers 3

10

Note the first condition - if (a[i] % 2 == 0) {a[i] += 1;} - this causes even values to be incremented. Therefore a[1] is incremented from 8 to 9.

Now, when i==3, a[a[i]] is evaluated to a[1] which is equal to 9. Then you are adding it to the original value of a[3] (note the operator is +=, not =), so a[3] becomes 1 + 9, which is 10.

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

3 Comments

I've just copied and pasted the above code and my IDE has evaluated a[3] to equal 10 and it's evaluating a[a[3]] to equal 9?
@Lorcan thanks. I was misreading the first condition. For some reason my head translated a[i] % 2 == 0 as i % 2 == 0, which made me think that condition doesn't modify a[1]. Fixed now.
Apologies, I was looking at the array as if it wasn't being updated as it was looping! Thanks!
2
int[] a = new int[] {9, 8, 3, 1, 5, 4};

for (int i = 0; i < a.length; i++){
    if (a[i] % 2 == 0)
        a[i] += 1;
    else if(a[i] < a.length)
        a[i] += a[a[i]];
}

|i | a[i] old | a[i] % 2 | a[i] < a.length | a[a[i]] | a[i] new |
|--|----------|----------|-----------------|---------|----------|
|0 |  9       |    -     |   -             |  NaN    |    9     |
|1 |  8       |    +     |   -             |  NaN    |    9     |
|2 |  3       |    -     |   +             |  1      |    4     |
|3 |  1       |    -     |   +             |  9      |    10    |
|4 |  5       |    -     |   +             |  4      |    9     |
|5 |  4       |    +     |   -             |  9      |    5     |

// a = {9, 9, 4, 10, 9, 5};

I thought that a[3] would equal 1

That's correct, at step 3, a[3] = 1

and then a[1] would equal 8

That's not correct, because at step 1 it was incremented to 9

Comments

1

Its okey. Best way to see what you are doing is debuggin(painting in this case) your code:

int[] a = new int[] {9, 8, 3, 1, 5, 4};

    for (int i = 0; i < a.length; i++){
        if (a[i] % 2 == 0){
            System.out.printf(a[i]);
            a[i] += 1;
            System.out.printf(a[i]);
        }else if(a[i] < a.length){
            System.out.printf(a[i]);
            a[i] += a[a[i]];
            System.out.printf(a[i]);
}

And you will see more clearly.Sometimes its better to face the wall by ourselves because one best practices is learn about you. Good luck!.

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.