4.10 Implementing ArrayList Algorithms
Implement algorithms with ArrayLists to search, count, calculate results, and update a resizable collection.
1. 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.lengthandget(i)for[i]. - Never add or remove inside an enhanced
forloop; it can throwConcurrentModificationException.
Picking an Algorithm
- Largest or smallest → start at
nums.get(0), loop from index1 - How many match → counting loop with
count++ - Keep only some → add matches to a new list
- Delete in place → loop from
size() - 1down to0
2. LxD Cycle Process
Empathize: Students know the single ArrayList methods but freeze when asked for “the largest score” or “how many match.” Removing while looping forward also skips elements with no error.
Define:
- POV: CSA students need to see
ArrayListalgorithms as the array loops they already know, withsize()andget(i)swapped in. - Learning Goal: Students will find a max or min, count matches, and remove elements while traversing without skipping any.
Ideate:
- HMW Question: How might we make a skipped element visible so the backward loop feels earned, not memorized?
- Activity: Analyze your own screen time in the popcorn hack, trace loops in the MCQ, then write
minValueandremoveNegatives.
Prototype:
- A reference guide, runnable Java examples, a personalized popcorn hack, an MCQ knowledge check, and a scaffolded grade rubric.
- Excellence means explaining why the accumulator starts at
get(0)and why removal runs backward.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers start at
get(0)and loop backward when removing. - On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
3. 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
ArrayListalgorithms 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
ArrayListrequires the use of special techniques to avoid skipping elements.” - 4.9.A.4 “Changing the size of an
ArrayListwhile traversing it using an enhanced for loop can result in aConcurrentModificationException.”
4. 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 (5 minutes)
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]
}
5. 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);
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);
6. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one raw cell at the top with the frontmatter:
Code Runner Challenge
Fill in the blanks, then run it
View IPYNB Source
---
layout: post
codemirror: true
title: Implementing ArrayList Algorithms HW
categories: [Java]
lesson_language: Java
lesson_topic: Implementing ArrayList Algorithms HW
lesson_part: interactive
lesson_type: lesson
permalink: /csa/unit_04/4_10-hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
- Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 4.10 Implementing ArrayList Algorithms
MCQ 4.10: <paste the copied result line, such as 3/4 | answers: C,B,C,B>
Popcorn: five values of my own, max and count printed (yes/no)
Homework: minValue output = <output>
Homework: removeNegatives output = <output>
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Run each cell and leave the output showing.
- Use your own values, not the sample answer.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
2-minute challenge: analyze five days of your screen time, then run the cell.
- Store the minutes in an
ArrayList<Integer> - Find the most minutes in one day
- Count the days with at least 120 minutes
Replace the sample values with your own.
// 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);
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 submission 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);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | Five values of your own, with the max and the count printed, and the cell runs. |
| MCQ | 0.2 | 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered. |
| Homework: min start | 0.15 | minValue starts at nums.get(0) and compares with <. |
| Homework: min output | 0.15 | Prints -5. |
| Homework: backward loop | 0.15 | removeNegatives loops from size() - 1 down to 0. |
| Homework: remove output | 0.15 | Prints [3, 0], with both adjacent negatives removed. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - MCQ score in the notes.
- Accumulator starts at
get(0), not0. - Removal loop runs backward.
7. Lesson Revisions
Revision Made: Put the correct and buggy max side by side in one runner so students see -2 and 0 next to each other instead of in separate cells. Changed the homework test list to [3, -1, -5, 0] so two negatives sit next to each other and a forward loop visibly fails. Cut the searching and filtering examples, since peers already knew indexOf and contains from 4.8, and moved them into the Reference Guide and MCQ.
8. Feedback Evidence
Feedback Received: In the practice run, 3 of 6 peers wrote removeNegatives with a forward loop and passed the old test list, because its negatives were never next to each other, so they never saw the bug. Two peers started min at 0 and said the buggy max example was easy to skip since it was in a separate cell. Peers said the searching and filtering examples repeated 4.8 and made the lesson too long for one class period.
9. 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.