Complete the Arrays of objects exercises before reviewing the solutions.

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

Original code

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

System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]

Coordinate2D[] coors2 = new Coordinate2D[3];

for(int i = 0; i < coors2.length; i++)
    coors2[i] = new Coordinate2D(coors[i].getX(), coors[i].getY());

coors2[0] = new Coordinate2D(4, 4);

coors2[1].setX(5);
coors2[1].setY(5);

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

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

Output

[(1, 1), (2, 2), (3, 3)]
[(1, 1), (2, 2), (3, 3)]
[(4, 4), (5, 5), (3, 3)]

Explanation

The statement coors2[i] = new Coordinate2D(coors[i].getX(), coors[i].getY()); makes a new Coordinate2D object containing the same values as the object referred to by coors[i] and stores the memory address of the new object in coors2[i].

At the end of the for loop coors2 refers to (store the memory addresses of) 3 new objects containing the same values as the objects to which coors refers. This is known as creating a deep copy of the array.

The statement coors2[0] = new Coordinate2D(4, 4); has the same effect as shown in the Exercise 1 solution.

The statements coors2[1].setX(5); and coors2[1].setY(5); still run the same mutator methods as in Exercise 1; however, coors2[1] refers to a different object than coors[1].

Step by step memory diagram

Step 1

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

System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]

Coordinate2D[] coors2 = new Coordinate2D[3];

// additional code not yet run

Memory diagram after Step 1

coors points to a box representing an array. The array points to 3 boxes representing Coordinate2D objects. The objects contain x: 1, y: 1, x: 2, y: 2, and x:3, y: 3. coors2 points to a different box representing a different array. The array contains 3 null values.

This is the same as in Exercise 1.

Output after Step 1

[(1, 1), (2, 2), (3, 3)]

This is also the same as in Exercise 1.

Step 2

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

System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]

Coordinate2D[] coors2 = new Coordinate2D[3];

for(int i = 0; i < coors2.length; i++)
    coors2[i] = new Coordinate2D(coors[i].getX(), coors[i].getY());

// additional code not yet run

Memory diagram after Step 2

[coors points to a box representing an array. The array points to 3 boxes representing Coordinate2D objects. The objects contain values x: 1, y: 1, x: 2, y: 2, and x: 3, y: 3. coors2 points to a different box representing a different array. The array points to 3 boxes representing Coordinate2D objects (completely different objects than those pointed to by coors). The objects contain values x: 1, y: 1, x: 2, y: 2, and x: 3, y: 3]

The statement coors2[i] = new Coordinate2D(coors[i].getX(), coors[i].getY()); makes a new Coordinate2D object containing the same values as the object referred to by coors[i] and stores the memory address of the new object in coors2[i].

At the end of the for loop coors2 refers to (store the memory addresses of) 3 new objects containing the same values as the objects to which coors refers. This is known as creating a deep copy of the array.

Step 3

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

System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]

Coordinate2D[] coors2 = new Coordinate2D[3];

for(int i = 0; i < coors2.length; i++)
    coors2[i] = new Coordinate2D(coors[i].getX(), coors[i].getY());

coors2[0] = new Coordinate2D(4, 4);

// additional code not yet run

Memory diagram after Step 3

coors points to a box representing an array. The array points to 3 boxes representing Coordinate2D objects. The objects contain values x: 1, y: 1, x: 2, y: 2, and x: 3, y: 3. coors2 points to a different box representing a different array. The array points to 3 boxes representing Coordinate2D objects (completely different objects than those pointed to by coors). The objects contain values x: 4, y: 4, x: 2, y: 2, and x: 3, y: 3

The statement coors2[0] = new Coordinate2D(4, 4); has the same effect as shown in the Exercise 1 solution. Specifically, coors2[0] points to (stores the memory address of) a new Coordinate2D object representing (4, 4). coors[0] remains unchanged.

Step 4

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

System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]

Coordinate2D[] coors2 = new Coordinate2D[3];

for(int i = 0; i < coors2.length; i++)
    coors2[i] = new Coordinate2D(coors[i].getX(), coors[i].getY());

coors2[0] = new Coordinate2D(4, 4);

coors2[1].setX(5);
coors2[1].setY(5);

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

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

Memory diagram after Step 4

coors points to a box representing an array. The array points to 3 boxes representing Coordinate2D objects. The objects contain values x: 1, y: 1, x: 2, y: 2, and x: 3, y: 3. coors2 points to a different box representing a different array. The array points to 3 boxes representing Coordinate2D objects (completely different objects than those pointed to by coors). The objects contain values x: 4, y: 4, x: 5, y: 5, and x: 3, y: 3

The statements coors2[1].setX(5); and coors2[1].setY(5); still run the same mutator methods as in Exercise 1. The x and y values inside the object to which coors2[1] refers are changed to 5 and 5.

Since coors[1] refers to a different object, the values inside object remain unchanged.

Output after Step 4

[(1, 1), (2, 2), (3, 3)]
[(1, 1), (2, 2), (3, 3)]
[(4, 4), (5, 5), (3, 3)]

Comments

Comment on Arrays of objects exercises