01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Parallel ArrayList vocabulary.

Used in record-management classes.

Vocabulary
  • 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.
04
AP Exam Focus

How parallel-ArrayList classes are tested.

Synchronization and search dominate.

Exam Focus
  • 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.
05
Worked Example

Trace Gradebook operations.

Two lists growing together.

Worked Example AP-style reasoning
  • 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.
06
Common Mistakes

Parallel-ArrayList pitfalls.

Breaking alignment is the top issue.

Watch Out
  • 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.
07
Reference Table

Parallel-ArrayList rules.

Sync invariants.

Parallel ArrayList Reference

AP Quick Reference
OperationBoth lists?Method used
addYesadd on both
update valueValue onlyset on value list
removeYesremove(i) on both
searchKey list onlyequals on each
08
Practice Tip

How to master parallel-ArrayList classes.

Touch both lists together; search the key.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 19 marks Pending

AP CSA Unit 13 Topic 09 Complete Class with Parallel ArrayLists FRQ Practice

AP-style topic practice assessment

Start