-4

I'm having trouble tracing some code to get the output:

int[] a = new int[5];
for (int i = 0; i < a.length; i++)
{
    a[i] = i*3-1;
}
    
    
for (int i = a.length-1; i >= 0; i--)
{
    System.out.print(a[i] + " " );
}
    

This outputs:

11 8 5 2 -1

What I don't understand is why the second variable 'i' is being put back in the formula from the first For Loop. How can I connect these two loops? The array is defined in the first loop, so why does it affect the second? I don't understand the relation, perhaps I'm just missing it.

halfer
  • 19,824
  • 17
  • 99
  • 186
j_b_kwik
  • 3
  • 4
  • "why the second variable 'i' is being put back in the formula from the first For Loop" What do you mean? Which output did you expect? – tkausl Feb 18 '17 at 23:06
  • The first loop populates the array to contain `[-1, 2, 5, 8, 11]`. The second loop just prints it backwards. – Andy Turner Feb 18 '17 at 23:06
  • The first loop obviously fills the array. The second loop displays the created array but it does it from the highest index (4) to the lowest index (0). The variable **i** merely holds the current index number of the array element to process. – DevilsHnd - 退職した Feb 18 '17 at 23:10
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Feb 18 '17 at 23:25

3 Answers3

0
for (int i = 0; i < a.length; i++) {
  a[i] = i*3-1;
}

In the first loop (above), i is the incrementer for the loop, which I assume you understand. Knowing that, we know that the values of i throughout the loop will be 0, 1, 2, 3, and 4.

For the statement a[i] = i * 3 - 1, it serves two purposes.

  1. An indexing value to give access to modify the values in the a array.
  2. Uses the incrementing values of i to calculate a new integer.

For example, on the first iteration, i will be 0. Therefore, the code inside of the loop will set the value at position 0 of the array to 0 * 3 - 1, or -1. This goes on for the remainder of the loop, explaining the values printed by the second loop as it prints it in reverse.

m_callens
  • 6,100
  • 8
  • 32
  • 54
0

What I don't understand is why the second variable 'i' is being put back in the formula from the first For Loop.

i is just the name of the variable. The variable i isn't significant here in calculating the values, it only serves as the loop counter. You can use j or any other name in any for loop and will still get the same output.

How can I connect these two loops? The array is defined in the first loop, so why does it affect the second?

The array is populated with values in the first loop. But note that the array is declared initially at the beginning of the program.

int[] a = new int[5];

This means that the array's scope is not just limited to that first for loop. It can be accessed anywhere within its scope.

So, in the first for loop, we are populating the array with values. And in the second for loop, we are displaying it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nikhil
  • 6,493
  • 10
  • 31
  • 68
  • The array may be populated in the first loop but it's not defined there. – pvg Feb 18 '17 at 23:29
  • By 'defining' I meant populating the values, as opposed to 'declaring'. Please check [this](https://stackoverflow.com/questions/11715485/what-is-the-difference-between-declaration-and-definition-in-java) SO question. – Nikhil Feb 18 '17 at 23:34
  • I've read it. It doesn't describe what you're talking about as 'definition'. If anything `int new[5]` is arguably a 'definition' although that's not the terminology the JLS uses. Either way, 'define' is not a good word to use for 'stuff things into an array in a loop' – pvg Feb 18 '17 at 23:40
  • I understand. I just used it in a casual sense. Since 'definition' isn't a formal term in Java, I'll remove it from my answer. Thanks! – Nikhil Feb 18 '17 at 23:45
0

The scope of 'i' is within the loop, so once a loop ends the variable 'i' no longer exists and can be re-instantiated. To understand look at this code:

public class Example{
    public static void main(String args[]){
        while(isTrue){
           int x += 3; // Int x exists here
        }
        //x does not exist here it is outside of scope so we can make another
        int x = 10;
    }
}

Var Scope