A class managing two parallel ArrayLists.
Names and scores. Items and prices. Two aligned collections governed by one class.
When the data is tabular — each record has a name and a value — two parallel ArrayLists as fields are an AP-friendly design. The class enforces alignment by routing all changes through its methods.
On the AP exam, this design appears in roster, vote-tally, and inventory FRQs.
Both lists modified together; lookups search the key list.
Invariant: equal sizes, aligned content.
Gradebook class
public class Gradebook {
private ArrayList<String> names;
private ArrayList<Integer> scores;
public Gradebook() {
names = new ArrayList<>();
scores = new ArrayList<>();
}
public void addStudent(String name, int score) {
names.add(name);
scores.add(score);
}
public int getScore(String name) {
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(name)) {
return scores.get(i);
}
}
return -1;
}
public void updateScore(String name, int newScore) {
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(name)) {
scores.set(i, newScore);
return;
}
}
}
public int size() { return names.size(); }
}
Alignment invariant
The two lists must always have the same size, and index i in each refers to the same student. Every method must preserve this.
Parallel ArrayList vocabulary.
Used in record-management classes.
- Parallel ArrayLists — two lists with aligned indices.
- Key list — the searchable list (names).
- Value list — the corresponding data (scores).
- Alignment — same size; same logical entity per index.
- Routing — all changes go through the class's methods.
How parallel-ArrayList classes are tested.
Synchronization and search dominate.
- Both lists added together. Never one without the other.
- Search the key list; act on value list.
- Use
.equals()for key comparison. - Sizes always equal. Sanity-check after every operation.
Trace Gradebook operations.
Two lists growing together.
- g.addStudent("Alex", 85) → names=[Alex], scores=[85].
- g.addStudent("Ben", 72) → names=[Alex, Ben], scores=[85, 72].
- g.getScore("Ben") → 72.
- g.updateScore("Alex", 90) → scores=[90, 72].
- g.getScore("Cara") → -1 (not found).
- g.size() → 2.
Parallel-ArrayList pitfalls.
Breaking alignment is the top issue.
- Adding to only one list. Breaks alignment.
- Removing from one independently. Same problem.
- Using
==for keys. Strings need.equals(). - Sorting one independently. Destroys record alignment.
Parallel-ArrayList rules.
Sync invariants.
Parallel ArrayList Reference
AP Quick Reference| Operation | Both lists? | Method used |
|---|---|---|
| add | Yes | add on both |
| update value | Value only | set on value list |
| remove | Yes | remove(i) on both |
| search | Key list only | equals on each |
How to master parallel-ArrayList classes.
Touch both lists together; search the key.
- MCQ Practice — verify both lists update together on add and remove.
- Java Lab — build Gradebook with addStudent, getScore, updateScore. Print both lists after every call.
- FRQ Practice — treat (names, scores) as one logical unit; touch them together always.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 09 Complete Class with Parallel ArrayLists FRQ Practice
AP-style topic practice assessment