Fiveable
Fiveable
AP Computer Science A
Find gaps with guided practice
Guided practice grid visualization
Table of Contents

💻ap computer science a review

7.3 Traversing ArrayLists

Verified for the 2025 AP Computer Science A examCitation:

Just like with arrays, we often need to process each element in an ArrayList. Traversing an ArrayList involves accessing each element one by one. In this guide, we'll explore different ways to traverse ArrayList objects and some important considerations to keep in mind when doing so.

Ways to Traverse an ArrayList

There are two main approaches for traversing an ArrayList. When traversing, you'll often use the size() method to determine how many elements are in the list, and possibly the remove() method if you need to delete elements during traversal.

1. Using a For Loop or While Loop

The traditional approach uses an indexed for loop or a while loop with a counter. This approach relies on the size() method to determine the boundaries of our loop:

ArrayList<String> names = new ArrayList<String>();
names.add("Taylor");
names.add("Morgan");
names.add("Jordan");

// Traversing with a for loop
for (int i = 0; i < names.size(); i++) {
    String name = names.get(i);
    System.out.println(name);
}

// Traversing with a while loop
int i = 0;
while (i < names.size()) {
    String name = names.get(i);
    System.out.println(name);
    i++;
}

Key points about this approach:

  • You have access to the index (position) of each element
  • You can modify elements using set(int index, E obj)
  • You need to be careful with bounds (0 to size()-1)

2. Using an Enhanced For Loop

The enhanced for loop (also called for-each loop) provides a cleaner syntax:

ArrayList<String> names = new ArrayList<String>();
names.add("Taylor");
names.add("Morgan");
names.add("Jordan");

// Traversing with enhanced for loop
for (String name : names) {
    System.out.println(name);
}

Key points about this approach:

  • Simpler, more readable code
  • No need to manage indices
  • No risk of ArrayIndexOutOfBoundsException
  • Limited ability to modify the ArrayList during traversal

Understanding ArrayList Indices

When working with ArrayList indices, keep these important points in mind:

  • Valid indices start at 0 and end at size() - 1

  • Accessing an index value outside this range will result in an ArrayIndexOutOfBoundsException

  • This is the same behavior as regular arrays

ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(10);
numbers.add(20);
numbers.add(30);

// Valid index access
System.out.println(numbers.get(0));  // 10
System.out.println(numbers.get(2));  // 30

// Invalid index access - will throw ArrayIndexOutOfBoundsException

// System.out.println(numbers.get(3));  // Error!
// System.out.println(numbers.get(-1)); // Error!

Special Considerations When Traversing ArrayLists

1. Deleting Elements During Traversal

Removing elements while traversing an ArrayList requires special care. The remove() method changes the size of the list and shifts elements, which can cause problems during traversal:

ArrayList<Integer> values = new ArrayList<Integer>();
values.add(5);
values.add(10);
values.add(15);
values.add(20);
values.add(25);

// INCORRECT approach - can cause problems

for (int i = 0; i < values.size(); i++) {
    if (values.get(i) > 15) {
        values.remove(i);
        // Problem: After removing, all elements shift left,
        // but the index i still increases, potentially skipping elements
    }
}

// BETTER approach - traverse backwards

for (int i = values.size() - 1; i >= 0; i--) {
    if (values.get(i) > 15) {
        values.remove(i);
    }
}

// ANOTHER approach - use an iterator (more advanced)

import java.util.Iterator;
Iterator<Integer> iter = values.iterator();
while (iter.hasNext()) {
    if (iter.next() > 15) {
        iter.remove();  // Safe way to remove during iteration
    }
}

2. Modifying Size During Enhanced For Loop Traversal

You should not add or remove elements from an ArrayList while traversing it with an enhanced for loop. This can result in a ConcurrentModificationException error:

ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

// This will cause a ConcurrentModificationException
try {
    for (String fruit : fruits) {
        if (fruit.equals("Banana")) {
            fruits.remove(fruit);  // DON'T do this!
        }
    }
} catch (Exception e) {
    System.out.println("Error: " + e);
}

When using an enhanced for loop to traverse an ArrayList, you should not:

  • Add new elements to the list
  • Remove elements from the list
  • Change the size of the list in any way

If you need to modify the ArrayList while traversing it, use a standard indexed for loop or an Iterator.

Safe Ways to Modify During Traversal

1. Using a Regular For Loop (Carefully)

ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(10);
numbers.add(15);
numbers.add(20);

// Remove even numbers
for (int i = numbers.size() - 1; i >= 0; i--) {
    if (numbers.get(i) % 2 == 0) {
        numbers.remove(i);
    }
}

Traversing backwards is safer because when an element is removed, the indices of elements that have already been processed don't change.

2. Using a Temporary List

ArrayList<String> names = new ArrayList<String>();
names.add("Alex");
names.add("Bailey");
names.add("Casey");

// Create a temporary list to store elements to remove
ArrayList<String> toRemove = new ArrayList<String>();

// First pass: identify elements to remove
for (String name : names) {
    if (name.startsWith("B")) {
        toRemove.add(name);
    }
}

// Second pass: remove the identified elements
names.removeAll(toRemove);

Common Traversal Patterns

Finding and Processing Elements

ArrayList<Student> students = new ArrayList<Student>();
// Assume students is populated with Student objects

// Find students with grade 'A'
for (Student student : students) {
    if (student.getGrade().equals("A")) {
        System.out.println(student.getName() + " earned an A");
    }
}

Counting Elements That Meet a Condition

ArrayList<Integer> scores = new ArrayList<Integer>();
// Assume scores is populated with test scores

int countPassing = 0;
for (int score : scores) {
    if (score >= 70) {
        countPassing++;
    }
}
System.out.println("Number of passing scores: " + countPassing);

Computing Statistics

ArrayList<Double> values = new ArrayList<Double>();
// Assume values is populated with numbers

double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;

for (double value : values) {
    sum += value;
    if (value > max) max = value;
    if (value < min) min = value;
}

double average = sum / values.size();
System.out.println("Average: " + average);
System.out.println("Maximum: " + max);
System.out.println("Minimum: " + min);

Summary

Traversing ArrayList objects can be done with traditional for/while loops or with the enhanced for loop. Each approach has its strengths: indexed loops provide access to positions and allow safe modification, while enhanced for loops offer cleaner syntax. Remember that indices work the same way as in arrays (0 to size()-1), and be careful about modifying an ArrayList during traversal to avoid exceptions. When you need to remove elements, consider using a backward traversal or a temporary collection to ensure safe and correct operation.

Key Terms to Review (10)

Add(): The add() method is used to insert an element into a data structure, such as a list or set.
ArrayList: ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.
ConcurrentModificationException: A ConcurrentModificationException occurs when a collection is modified while being iterated over using an iterator or enhanced for loop.
Enhanced For Loop: An enhanced for loop (also known as a foreach loop) is a simplified way to iterate over elements in an array or collection. It automatically handles indexing and provides an easy way to access each element without explicitly using indices.
For Loop: A for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of times or until certain conditions are met.
Get(): The get() method is used to retrieve the value of an element at a specific index in a list or array.
IndexOutOfBoundsException: IndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position within an array or collection.
Remove(): The remove() method removes an element from a collection, if it exists, and returns true. If the element doesn't exist, it returns false.
Set(): A set is a collection of unique elements with no specific order. It does not allow duplicate values.
Size(): The size() method is used to determine the total number of elements present in a collection, such as a list or array.