Complete the Enhanced for loop exercises before reviewing the solutions.

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

Original code

Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(0, 0);
coors[1] = new Coordinate2D(1, 1);
coors[2] = new Coordinate2D(2, 2);

for(Coordinate2D c : coors)
{
    System.out.print(c + " ");
    c = new Coordinate2D(-1, -1);
    System.out.println(c);
}

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

Output

(0, 0) (-1, -1)
(1, 1) (-1, -1)
(2, 2) (-1, -1)
[(0, 0), (1, 1), (2, 2)]

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 (c in this code segment) do not change the values in the array.

Step by step memory diagram

Step 1

Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(0, 0);
coors[1] = new Coordinate2D(1, 1);
coors[2] = new Coordinate2D(2, 2);

// more code not yet run

Memory diagram after Step 1

The diagram shows coors pointing to a box representing the array. Each spot in the array points to a box representing a Coordinate2D object. The first object contains x: 0, y: 0. The second object contains x: 1, y: 1. The third object contains x: 2, y: 2.

An array of objects is really an array of references. Each position in coors stores the memory address of a Coordinate2D object (the arrow pointing to the object). The array itself is also an object so coors stores the memory address of the array.

Step 2

Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(0, 0);
coors[1] = new Coordinate2D(1, 1);
coors[2] = new Coordinate2D(2, 2);

for(Coordinate2D c : coors)
{
    System.out.print(c + " ");
    // 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 coors pointing to a box representing the array. Each spot in the array points to a box representing a Coordinate2D object. The first object contains x: 0, y: 0. The second object contains x: 1, y: 1. The third object contains x: 2, y: 2. The diagram shows the variable c pointing to the box representing the Coordinate2D object that contains x: 0, y: 0.

The first time the enhanced for loop runs, c is set to a copy of the value at coors[0]. The value at coors[0] is the memory address of the object containing x: 0, y: 0 (the arrow). In other words, c and coors[0] point to the same object.

Output after Step 2

(0, 0) 

The print statement prints the object to which c points, which is the same object to which coors[0] points. The object contains x: 0, y: 0.

Step 3

Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(0, 0);
coors[1] = new Coordinate2D(1, 1);
coors[2] = new Coordinate2D(2, 2);

for(Coordinate2D c : coors)
{
    System.out.print(c + " ");
    c = new Coordinate2D(-1, -1);
    System.out.println(c);
}

// 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 coors pointing to a box representing the array. Each spot in the array points to a box representing a Coordinate2D object. The first object contains x: 0, y: 0. The second object contains x: 1, y: 1. The third object contains x: 2, y: 2. The diagram shows the variable c pointing to a box representing a Coordinate2D object that contains x: -1, y: -1. Nothing else points to the box representing the Coordinate2D object that contains x: -1, y: -1.

The statement c = new Coordinate2D(-1, -1); makes a new object/instance of Coordinate2D and sets c to the memory address of the new object. This changes only the value of c. The value of the coors[0] is not changed, nor is anything about the object to which coors[0] points.

Output after Step 3

(0, 0) (-1, -1)

The println statement prints the object to which c points. The object contains x: -1, y: -1.

Step 4

Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(0, 0);
coors[1] = new Coordinate2D(1, 1);
coors[2] = new Coordinate2D(2, 2);

for(Coordinate2D c : coors)
{
    System.out.print(c + " ");
    c = new Coordinate2D(-1, -1);
    System.out.println(c);
}

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

Step 4 is after the entire code segment has executed.

Memory diagram after Step 4

The diagram shows coors pointing to a box representing the array. Each spot in the array points to a box representing a Coordinate2D object. The first object contains x: 0, y: 0. The second object contains x: 1, y: 1. The third object contains x: 2, y: 2.

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

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

Output after Step 4

(0, 0) (-1, -1)
(1, 1) (-1, -1)
(2, 2) (-1, -1)
[(0, 0), (1, 1), (2, 2)]

Each time the loop runs, the code segment prints the object to which c originally referred and the (different) object to which c refers after the assignment statement.

The println statement outside the loop prints the entire array, which contains its original values. Each object to which the array points also 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