Parallel arrays with conditional construction.
Two aligned arrays drive a third — built only from rows that satisfy a condition.
Parallel arrays share an index: index i in names[] and index i in scores[] refer to the same logical entity. Conditional construction builds a new array (or pair of arrays) containing only the entries that meet a test.
Because the result list's size isn't known up front, you either count first to allocate, or use an ArrayList and convert.
On the AP exam, parallel arrays appear in pre-object-oriented problems and as a stepping stone toward ArrayLists of objects.
Maintain alignment; build the result with a running index.
Two passes: count, then build.
Two-pass build (fixed-size result)
public String[] passingNames(String[] names, int[] scores, int threshold) {
int count = 0;
for (int s : scores) if (s >= threshold) count++;
String[] result = new String[count];
int j = 0;
for (int i = 0; i < names.length; i++) {
if (scores[i] >= threshold) {
result[j] = names[i];
j++;
}
}
return result;
}
First pass counts; second pass copies. The variable j tracks the next free position in result.
Alignment is sacred
Whatever happens to one array must happen to the other at the same index. Sorting one independently breaks the relationship.
Parallel-array vocabulary.
Used in indexed-record problems.
- Parallel arrays — aligned by index.
- Logical record — combined data at a shared index.
- Conditional build — constructing result only from qualifying entries.
- Running index — counter tracking the next free position in the result.
- Two-pass build — count then copy.
How parallel arrays are tested.
Alignment and result-size dominate.
- Alignment. Never sort or modify one without the other.
- Result size. Must equal the count of matches.
- Running index. Increment only when adding to result.
- Source untouched. Conditional build is non-destructive.
Build passing names.
Two passes; trace each.
Input: names = ["Alex", "Ben", "Cara", "Dax"], scores = [85, 50, 72, 40], threshold 60.
Pass 1 — count: 85, 72 qualify → count = 2.
Pass 2 — build:
- i=0: 85 ≥ 60 → result[0] = "Alex"; j=1.
- i=1: 50 < 60 → skip.
- i=2: 72 ≥ 60 → result[1] = "Cara"; j=2.
- i=3: 40 < 60 → skip.
Result: ["Alex", "Cara"].
Parallel-array pitfalls.
Most break alignment or miscount.
- Allocating wrong-size result. Must equal count from pass 1.
- Running
jalways. Increment only when adding. - Using
iinstead ofjin result. Leaves gaps or overwrites. - Modifying source arrays. Breaks alignment.
Parallel-array build template.
Two passes; running index.
Parallel Build Reference
AP Quick Reference| Step | Action | Variable |
|---|---|---|
| 1 | Count matches | count |
| 2 | Allocate result | size = count |
| 3 | Copy matches | j (running index) |
| 4 | Return result | — |
How to master parallel arrays.
Count first; copy second.
- MCQ Practice — verify
jincrements only on match. - Java Lab — implement passingNames, expensiveItems with parallel arrays. Verify alignment.
- FRQ Practice — write the two-pass count-then-build pattern; never sort one array independently.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 13 Parallel Arrays and Conditional Build FRQ Practice
AP-style topic practice assessment