Reference Guide

Key Topics


Term Definition Example
Traversal Visiting every element with a loop. for (int i = 0; i < nums.size(); i++)
Accumulator A variable that builds a result across the loop. max, count
indexOf Index of the first match, or -1. roster.indexOf("Amir")
contains true if any element matches. roster.contains("Amir")
  • Same loop shape as arrays, with size() for .length and get(i) for [i].
  • Never add or remove inside an enhanced for loop; it can throw ConcurrentModificationException.

Picking an Algorithm

  • Largest or smallest → start at nums.get(0), loop from index 1
  • How many match → counting loop with count++
  • Keep only some → add matches to a new list
  • Delete in place → loop from size() - 1 down to 0

College Board Requirements

AP CSA Unit 4, Topic 4.10 Implementing ArrayList Algorithms. Quoted from the course and exam description (College Board, 2025, pp. 112-113):

  • 4.10.A.1 “There are standard ArrayList algorithms that utilize traversals to: determine a minimum or maximum value; … determine the number of elements having a particular property; … delete elements.”
  • 4.9.A.2 “Deleting elements during a traversal of an ArrayList requires the use of special techniques to avoid skipping elements.”
  • 4.9.A.4 “Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException.”

Lesson Plan

Learning Objective: Write loops over an ArrayList to find extremes, count matches, and safely remove elements.

Success Criteria: You can explain why max starts at get(0) and why removal loops run backward.

Tech Talk

An algorithm is a loop that combines single methods to answer a question: What is the highest score? How many passed? Which ones should go?

Code Runner Challenge

Predict both results before running, then change the list to all positive numbers

View IPYNB Source
int max = nums.get(0);                        // start with a real element
for (int i = 1; i < nums.size(); i++) {       // size(), not .length
    if (nums.get(i) > max) max = nums.get(i); // get(i), not nums[i]
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Examples

A. Finding a maximum. The MaxFinder class compares a correct start to a buggy one

Common starting mistake:


int max = 0;             // wrong when every value is negative
int max = nums.get(0);   // always a real element

Terminology:

max is an accumulator: the best value seen so far. Flip > to < and the same loop finds a minimum.


Code Runner Challenge

Predict both lists before running, then trace the forward loop to find the skipped 2

View IPYNB Source
// CODE_RUNNER: Predict both results before running, then change the list to all positive numbers
// Correct vs. buggy maximum
import java.util.ArrayList;

public class MaxFinder {
    public static int maxValue(ArrayList<Integer> nums, int start) {
        int max = start;
        for (int i = 0; i < nums.size(); i++) {
            if (nums.get(i) > max) {
                max = nums.get(i);
            }
        }
        return max;
    }

    public static void main(String[] args) {
        ArrayList<Integer> temps = new ArrayList<Integer>();
        temps.add(-4);
        temps.add(-9);
        temps.add(-2);

        System.out.println("start at get(0): " + maxValue(temps, temps.get(0)));
        System.out.println("start at 0:      " + maxValue(temps, 0));
    }
}

MaxFinder.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

B. The removal trap. The RemoveTwos class removes while looping forward and backward

Common removal mistake:


for (int i = 0; i < list.size(); i++)        // forward: skips after each removal
for (int i = list.size() - 1; i >= 0; i--)   // backward: never skips

Terminology:

remove(i) shifts later elements left. Going forward, the next element slides into i just as the loop moves on. Going backward, only already-checked elements move.


Code Runner Challenge

Replace the sample values with your own, then run it

View IPYNB Source
// CODE_RUNNER: Predict both lists before running, then trace the forward loop to find the skipped 2
// Forward vs. backward removal
import java.util.ArrayList;

public class RemoveTwos {
    public static ArrayList<Integer> makeList() {
        ArrayList<Integer> values = new ArrayList<Integer>();
        values.add(5);
        values.add(2);
        values.add(2);
        values.add(9);
        return values;
    }

    public static void main(String[] args) {
        ArrayList<Integer> forward = makeList();
        for (int i = 0; i < forward.size(); i++) {
            if (forward.get(i) == 2) forward.remove(i);
        }
        System.out.println("forward  = " + forward);

        ArrayList<Integer> backward = makeList();
        for (int i = backward.size() - 1; i >= 0; i--) {
            if (backward.get(i) == 2) backward.remove(i);
        }
        System.out.println("backward = " + backward);
    }
}

RemoveTwos.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Hacks & Practice Tasks

Popcorn Hack

2-minute challenge: analyze five days of your screen time, then run the cell.

  1. Store the minutes in an ArrayList<Integer>
  2. Find the most minutes in one day
  3. Count the days with at least 120 minutes

Replace the sample values with your own.

Code Runner Challenge

Fill in the blanks, then run it

View IPYNB Source
// CODE_RUNNER: Replace the sample values with your own, then run it
// Practice #1 - Try it yourself first!
import java.util.ArrayList;

public class MyScreenTime {
    public static void main(String[] args) {
        // Try writing your own code here first!

        // Sample answer:
        ArrayList<Integer> minutes = new ArrayList<Integer>();
        minutes.add(95); minutes.add(140); minutes.add(80); minutes.add(180); minutes.add(120);

        int max = minutes.get(0);
        int count = 0;
        for (int i = 0; i < minutes.size(); i++) {
            if (minutes.get(i) > max) max = minutes.get(i);
            if (minutes.get(i) >= 120) count++;
        }
        System.out.println("Most: " + max + ", days at 120+: " + count);
    }
}

MyScreenTime.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ Check

4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your notes.

Homework Hack

Task: Write two algorithms. minValue returns the smallest value, modeled after maxValue. removeNegatives removes every negative value in place without skipping any.

Solution Skeleton:

// CODE_RUNNER: Fill in the blanks, then run it
import java.util.ArrayList;

public class ListAlgorithms {
    // 1. Start at get(0), loop from index 1, compare with <
    public static int minValue(ArrayList<Integer> nums) {
        return 0;
    }

    // 2. Loop from size() - 1 down to 0 and remove(i) each negative
    public static void removeNegatives(ArrayList<Integer> nums) {
    }

    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(3); nums.add(-1); nums.add(-5); nums.add(0);

        System.out.println(minValue(nums));   // expect -5

        removeNegatives(nums);
        System.out.println(nums);             // expect [3, 0]
    }
}

ListAlgorithms.main(null);

References

College Board. (2025). AP Computer Science A course and exam description (Effective fall 2025). Topic 4.10 Implementing ArrayList Algorithms, pp. 120

Runestone Academy. (n.d.). Topic 4.10: Implementing ArrayList algorithms. In CSAwesome2.

All Java lessons