Complete the Enhanced for loop exercises before reviewing the solutions.

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

Original code

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

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

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

Output

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

Explanation

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

Each time the inner loop runs, val is set to a copy of a value from the 1D array to which matrix[r] refers.

Changing val does not change the value in matrix[r].

Step by step memory diagram

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.

Step 2

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

for(int r = 0; r < matrix.length; r++)
{
    for(int val : matrix[r])
    {
        System.out.print(val + " ");
        // 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. val stores 10.

The value of r is 0.

The value of val is set to a copy of the value of matrix[0], which is 10.

Output after Step 2

10 

The print statement prints val, which stores 10.

Step 3

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

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

// 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 10, 11, 12. The second spot in the array points to a box representing an array containing 13, 14, 15. r stores 0. val stores -99.

The statement val = -99; changes only the value of of val. There is no link between val and the values in matrix or the 1D arrays referenced by matrix.

This is the same situation as in Exercise 4. Using a regular for loop as the outer loop does not change anything.

Output after Step 3

10 -99

The println statement prints val, which stores -99.

Step 4

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

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

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 10, 11, 12. The second spot in the array points to a box representing an array containing 13, 14, 15.

This is the same final state as in Exercise 4. Except for r, the explanation is also the same.

The scope of r is the outer loop. It does not exist after the outer loop finishes.

Output after Step 4

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

This is the same final output as for Exercise 4. The explanation is also the same.

Comments

Comment on Enhanced for loop exercises