Complete the Enhanced for loop exercises before reviewing the solutions.

Review the exercise 1 solution with AP CS Tutor Brandon Horn.

Original code

int[] vals = new int[] {5, 7, 9};

for(int v : vals)
{
    System.out.print(v + " ");
    v = -1;
    System.out.println(v);
}

System.out.println(Arrays.toString(vals));

Output

5 -1
7 -1
9 -1
[5, 7, 9]

Explanation

Each time an enhanced for loop runs, the loop variable is set to a copy of the value in the array. Changes to the value of the loop variable (v in this code segment) do not change the values in the array.

Step by step memory diagram

Step 1

int[] vals = new int[] {5, 7, 9};

Memory diagram after Step 1

The diagram shows vals pointing to a box that represents the array. The box contains 5, 7, and 9, the values inside the array.

All arrays in Java are objects, regardless of the type of the values inside. vals stores the memory address of the array (the arrow) just as it would for any other object. The values inside the array are primitive types and are stored directly in the array.

Step 2

int[] vals = new int[] {5, 7, 9};

for(int v : vals)
{
    System.out.print(v + " ");
    // more code not yet run
}

// more code not yet run

Step 2 is inside the first iteration of the enhanced for loop, immediately after the print statement has been run.

Memory diagram after Step 2

The diagram shows vals pointing to a box that represents the array. The box contains 5, 7, and 9, the values inside the array. The diagram also shows v, separately from the array, storing the value 5.

Each time the loop runs, v is set to a copy of a value in vals. The first time the loop runs, v is set to 5.

Output after Step 2

5 

Step 3

int[] vals = new int[] {5, 7, 9};

for(int v : vals)
{
    System.out.print(v + " ");
    v = -1;
    System.out.println(v);
}

// more code not yet run

Step 3 is inside the first iteration of the enhanced for loop, immediately after the println statement has been run.

Memory diagram after Step 3

The diagram shows vals pointing to a box that represents the array. The box contains 5, 7, and 9, the values inside the array. The diagram also shows v, separately from the array, storing the value -1.

It is possible to change the value of v within the loop. The statement v = -1; really does set v to -1; however, it does not change the corresponding value in vals.

Output after Step 3

5 -1

The println statement prints the value of v, which is -1.

Step 4

int[] vals = new int[] {5, 7, 9};

for(int v : vals)
{
    System.out.print(v + " ");
    v = -1;
    System.out.println(v);
}

System.out.println(Arrays.toString(vals));

Step 4 is after the entire code segment has executed.

Memory diagram after Step 4

The diagram shows vals pointing to a box that represents the array. The box contains 5, 7, and 9, the values inside the array.

After the loop, the values in the array to which vals points remain unchanged.

The scope of v is the loop. v does not exist after the loop finishes.

Output after Step 4

5 -1
7 -1
9 -1
[5, 7, 9]

Each time the loop runs, the code segment prints the original value of v and the changed value.

The println statement outside the loop prints the entire array, which contains its original values.

Note: Arrays.toString returns a formatted String containing the values in its array parameter. Arrays.toString is not part of the AP CS A Java Subset.

Comments

Comment on Enhanced for loop exercises