Complete the Enhanced for loop exercises before reviewing the solutions.
Review the exercise 4 solution with AP CS Tutor Brandon Horn.
Original code
int[][] matrix = {
{10, 11, 12},
{13, 14, 15}
};
for(int[] row : matrix)
{
for(int val : row)
{
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, row
is set to a reference to a 1D array in matrix
.
Each time the inner loop runs, val
is set to a copy of the value from the 1D array to which row
refers.
Changing val
does not change the value in row
.
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
Each value in matrix is the memory address of a 1D array representing a row. Thinking of each memory address as the arrow works well. The values inside each row are each of primitive type int
.
Step 2
int[][] matrix = {
{10, 11, 12},
{13, 14, 15}
};
for(int[] row : matrix)
{
for(int val : row)
{
System.out.print(val + " ");
// more code not yet run
}
}
// more code not yet run
Step 2 is inside the inner enhanced for
loop, immediately after the print
statement has been run.
Memory diagram after Step 2
The value of row
is set to a copy of the value of matrix[0]
. The value of matrix[0]
is the memory address of the array containing 10, 11, 12
(the arrow). Both matrix[0]
and row
point to the same 1D array.
The value of val
is set to a copy of the value of row[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[] row : matrix)
{
for(int val : row)
{
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 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
.
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[] row : matrix)
{
for(int val : row)
{
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
After the loops finish, the values in matrix
and values in the arrays to which matrix
points remain unchanged.
The scope of val
is the inner loop. The scope of row
is the outer loop. Neither row
nor val
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]]
Each time the inner loop runs, the code segment prints the original and changed values of val
.
The println
statement outside the loop prints the entire array, which contains its original values. Each 1D array to which the array points also contains its original values.
Note: Arrays.deepToString
returns a formatted String
containing the values in its array parameter. Arrays.deepToString
is not part of the AP CS A Java Subset.