Enhanced for loops on the AP CS A Exam

Java enhanced for loops, also known as for each loops, can be used to traverse data structures. Structures that can be traversed with enhanced for loops include:

Multiple choice questions on the AP CS A Exam may use enhanced for loops. Students must be familiar with both the basic operation and the limitations illustrated in the exercises below.

On the AP CS A Exam, nothing on the Free Response section will require the use of enhanced for loops. All data structures used in AP CS A are indexed, so they can be traversed using regular for loops or while loops.

Using enhanced for loops on the AP CS A Free Response is fine as long as they are appropriate for the problem. When in doubt, use a regular for loop or a while loop. Common examples in which an enhanced for loop is not appropriate include:

(There are data structures in Java in which the elements do not have indexes, such as a Set. Some of these data structures require the use of an enhanced for loop or an Iterator. None of these data structures are included on the AP CS A Exam.)

The exercises below assume familiarity with objects and references (object variables) that store memory addresses. See the Primitive types vs references exercises.

Coordinate2D class

The exercises below use the Coordinate2D class. There are no tricks in the Coordinate2D class. Each method does what its header suggests.

Selected parts of Coordinate2D are shown below.

A link to the complete class is below.

Coordinate2D.java

For each exercise, give the output of the code segment.

Exercise 1: Enhanced for loop with array of primitive type

int[] vals = new int[] {5, 7, 9};

for(int v : vals)
{
    System.out.print(v + " ");
    v = -1;
    System.out.println(v);
}

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

Enhanced for loop exercise 1 solution & explanation

Exercise 2: Enhanced for loop with array of references

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

for(Coordinate2D c : coors)
{
    System.out.print(c + " ");
    c = new Coordinate2D(-1, -1);
    System.out.println(c);
}

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

Enhanced for loop exercise 2 solution & explanation

Exercise 3: Enhanced for loop with array of references

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

for(Coordinate2D d : coors)
{
    System.out.print(d + " ");
    d.setX(-1);
    System.out.println(d);
}

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

Enhanced for loop exercise 3 solution & explanation

Exercise 4: Enhanced for loops with 2D array

Exercises 4 - 6 use a 2D array. See Intro to 2D arrays for details of 2D arrays in Java.

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));

Enhanced for loop exercise 4 solution & explanation

Exercise 5: Mixed loop types with 2D array

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));

Enhanced for loop exercise 5 solution & explanation

Exercise 6: Mixed loop types with 2D array

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));

Enhanced for loop exercise 6 solution & explanation

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Enhanced for loop exercises