Holding object state in parallel arrays.
When a class needs to track many related records but objects-of-objects feel too heavy, parallel arrays inside an instance variable provide a clean alternative.
A class can hold parallel arrays as instance variables to represent a collection of related records. Index i in each array refers to the same logical entity — like a row in a database table spread across multiple columns.
This is the AP CSA stepping stone toward ArrayList<CustomObject>. It's especially common in FRQs that pre-date object-of-object designs or where the data is naturally tabular.
The discipline is the same as standalone parallel arrays: every operation must preserve alignment, and the class is responsible for enforcing that invariant.
Two arrays, one logical record per index.
Every operation touches both arrays together.
Class skeleton
public class Gradebook {
private String[] names;
private int[] scores;
private int size; // number of valid entries
public Gradebook(int capacity) {
names = new String[capacity];
scores = new int[capacity];
size = 0;
}
}
The size field tracks how many slots are actually used — distinct from the array's length (capacity).
Add a record (preserves alignment)
public void addStudent(String name, int score) {
if (size < names.length) {
names[size] = name;
scores[size] = score;
size++;
}
}
Both arrays get the new value at the same index. Both increase by one logical entry.
Look up a record
public int getScore(String targetName) {
for (int i = 0; i < size; i++) {
if (names[i].equals(targetName)) {
return scores[i];
}
}
return -1; // not found
}
Search the key array; return the corresponding value at the matched index.
The alignment invariant
The class promises that names[i] and scores[i] always describe the same student. Every method must preserve this. Adding to one array without the other, sorting one independently, or removing from only one breaks the invariant — and the class is broken from that point on.
Parallel-arrays-in-class vocabulary.
Used in tabular-data class FRQs.
- Parallel arrays — multiple arrays sharing an index.
- Logical record — combined data at a shared index.
- Invariant — property the class promises to maintain.
- Size vs capacity — count of valid entries vs total array length.
- Synchronization — keeping arrays aligned through every operation.
- Encapsulation — arrays are private; clients use methods.
How parallel-arrays classes are tested.
Alignment and size discipline are the rubric points.
- Every modification touches both arrays. Adding, removing, sorting.
- Loop with
i < size, noti < names.length. - Increment size after appending to both arrays.
- Use
.equals()for String key comparison.
MCQ patterns: trace contents of both arrays after several operations; identify which method preserves the invariant.
Trace operations on a Gradebook.
Both arrays grow together.
Setup. Gradebook g = new Gradebook(5); — both arrays length 5, size 0.
Operations:
g.addStudent("Alex", 85)→ names = [Alex, _, _, _, _]; scores = [85, 0, 0, 0, 0]; size = 1.g.addStudent("Ben", 72)→ names = [Alex, Ben, _, _, _]; scores = [85, 72, 0, 0, 0]; size = 2.g.addStudent("Cara", 91)→ size = 3.g.getScore("Ben")→ loop finds "Ben" at index 1; returns scores[1] = 72.g.getScore("Zoe")→ loop runs 0 to 2, no match; returns -1.
Why this matters. The size field controls where new entries land and where lookups stop. Without it, the loop would scan uninitialized slots, returning wrong results.
Parallel-arrays-class pitfalls.
Most break alignment or confuse size with capacity.
- Adding to only one array. Breaks alignment immediately.
- Looping to
names.lengthinstead ofsize. Scans uninitialized slots. - Forgetting to increment
size. Next add overwrites the just-added entry. - Sorting one array independently. Destroys the record alignment.
- Returning early from add without updating size. Leaves the arrays out of sync.
- Using
==for String keys. Use.equals().
Parallel-arrays class essentials.
Operations and their rules.
Parallel Class Reference
AP Quick Reference| Operation | What touches both | Loop bound |
|---|---|---|
| Add | Both, at index size | — |
| Lookup | Search key; return value | i < size |
| Remove | Shift both leftward | — |
| Sort | Together (swap both) | i < size |
How to master parallel-arrays classes.
After every method, verify both arrays match at every used index.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each option, verify both arrays change together when adding or removing.
- Java Lab — build a Gradebook with addStudent, getScore, getAverage. Print both arrays after every call to verify alignment.
- FRQ Practice — treat the two arrays as one logical structure. Touch them together every time.
If your reflex after every modification is "did both arrays change?", you've mastered the invariant.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 10 Topic 01 Object State with Parallel Arrays FRQ Practice
AP-style topic practice assessment