Complete the Arrays as objects exercises before reviewing the solutions.

Review the Arrays as objects exercise 3 solution with AP CS Tutor Brandon Horn.

Original code

public static void main(String[] args)
{
    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)]

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

public static void mystery3(Coordinate2D[] coorsInside)
{
    coorsInside[0] = new Coordinate2D(4, 4);
}

Output

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

Explanation

The mechanics are identical to those in Exercise 1.

The call to mystery3 passes the memory address of the array. Within mystery3, coors and coorsInside point to the same array.

Step by step memory diagram

Step 1

public static void main(String[] args)
{
    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)]

    // additional code not yet run
}

Memory diagram after Step 1

coors points to a box representing the 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

Output after Step 1

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

Step 2

public static void main(String[] args)
{
    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)]

    mystery3(coors);
    
    // additional code not yet run
}

public static void mystery3(Coordinate2D[] coorsInside)
{
    // additional code not yet run
}

Memory diagram after Step 2

coors and coorsInside point to the same box representing the array. The values in the array are the same as in step 1

As in Exercise 1, coors and coorsInside point to (store the memory address of) the same array.

Output after Step 2

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

Step 3

public static void main(String[] args)
{
    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)]

    mystery3(coors);
    
    // additional code not yet run
}

public static void mystery3(Coordinate2D[] coorsInside)
{
    coorsInside[0] = new Coordinate2D(4, 4);
}

Memory diagram after Step 3

coors and coorsInside point to the same box representing the array. The array points to 3 boxes representing Coordinate2D objects. The objects contain x: 4, y: 4, x: 2, y: 2, and x:3, y: 3

As in Exercise 1, the actual value inside the array to which both coorsInside and coors point is changed.

Output after Step 3

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

Step 4

public static void main(String[] args)
{
    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)]

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

Memory diagram after Step 4

coors points to a box representing the array. The array points to 3 boxes representing Coordinate2D objects. The objects contain x: 4, y: 4, x: 2, y: 2, and x:3, y: 3

As in Exercise 1, coorsInside goes away when mystery3 returns. coors points to the original array; however, the reference at index 0 now points to a Coordinate2D object containing (4, 4).

Comments

Comment on Arrays as objects exercises