Complete the Enhanced for loop exercises before reviewing the solutions.

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

Original code

int[][] matrix = {
        {10, 11, 12},
        {13, 14, 15}
};

for(int[] row : matrix)
{
    for(int i = 0; i < row.length; i++)
    {
        System.out.print(row[i] + " ");
        row[i] = -99;
        System.out.println(row[i]);
    }
}

System.out.println(Arrays.deepToString(matrix));

Output

10 -99
11 -99
12 -99
13 -99
14 -99
15 -99
[[-99, -99, -99], [-99, -99, -99]]

Explanation

A 2D array is an array of references to 1D arrays. Each time the outer loop runs, row is set to a reference to a 1D array in matrix.

Each time the inner loop runs, i is set to an index in row. matrix and row point to the same 1D array. Changes to the values in row change the values in matrix.

Step 1

int[][] matrix = {
        {10, 11, 12},
        {13, 14, 15}
};

// more code not yet run

Memory diagram after Step 1

The diagram shows matrix pointing to a box representing an array. The first spot in the array points to a box representing an array containing 10, 11, 12. The second spot in the array points to a box representing an array containing 13, 14, 15.

This is the same initial state as in Exercise 4 Step 1 and Exercise 5 Step 1.

Step 2

int[][] matrix = {
        {10, 11, 12},
        {13, 14, 15}
};

for(int[] row : matrix)
{
    for(int i = 0; i < row.length; i++)
    {
        System.out.print(row[i] + " ");
        // more code not yet run
    }
}

// more code not yet run

Step 2 is inside the inner loop, immediately after the print statement has been run.

Memory diagram after Step 2

The diagram shows matrix pointing to a box representing an array. The first spot in the array points to a box representing an array containing 10, 11, 12. The second spot in the array points to a box representing an array containing 13, 14, 15. r stores 0. row also points to the same box representing the array containing 10, 11, 12. i stores 0.

This step diverges significantly from Exercises 4 & 5. At this step, row points the same array as matrix[0].

i stores 0, and represents an index in the array to which row points.

Since each 1D array to which matrix points represents a row in the 2D array, i can also be thought of as a column index.

Output after Step 2

10 

The print statement prints row[0], which stores 10.

Step 3

int[][] matrix = {
        {10, 11, 12},
        {13, 14, 15}
};

for(int[] row : matrix)
{
    for(int i = 0; i < row.length; i++)
    {
        System.out.print(row[i] + " ");
        row[i] = -99;
        System.out.println(row[i]);
    }
}

// more code not yet run

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

Memory diagram after Step 3

The diagram shows matrix pointing to a box representing an array. The first spot in the array points to a box representing an array containing -99, 11, 12. The second spot in the array points to a box representing an array containing 13, 14, 15. r stores 0. row also points to the same box representing the array containing -99, 11, 12. i stores 0.

The statement row[i] = -99; changes the value at position i (0 at this step) in the array to which row points to -99. row points to the same array as matrix[0]. Changes to the values in row change the values in matrix.

This is similar to Exercise 3. All arrays in Java are objects. A variable of array type stores the memory address of the array object (the arrow). Changing a value within an array is equivalent to running a mutator method on an object.

Output after Step 3

10 -99

The print statement prints row[0], which stores 10.

Step 4

int[][] matrix = {
        {10, 11, 12},
        {13, 14, 15}
};

for(int[] row : matrix)
{
    for(int i = 0; i < row.length; i++)
    {
        System.out.print(row[i] + " ");
        row[i] = -99;
        System.out.println(row[i]);
    }
}

System.out.println(Arrays.deepToString(matrix));

Step 4 is after the entire code segment has executed.

Memory diagram after Step 4

The diagram shows matrix pointing to a box representing an array. The first spot in the array points to a box representing an array containing -99, -99, -99. The second spot in the array points to a different box representing a different array also containing -99, -99, -99.

All of the values in matrix have been changed to -99.

The scope of row is the outer loop. The scope of i is the inner loop. Neither variable exists after the outer loop finishes.

Output after Step 4

10 -99
11 -99
12 -99
13 -99
14 -99
15 -99
[[-99, -99, -99], [-99, -99, -99]]

All of the values in matrix are -99.

Comments

Comment on Enhanced for loop exercises