Insertion, removal, and search in one workflow.
Real list manipulation combines all three: find the position, then add or remove based on what was found.
Most AP-style list problems don't use a single ArrayList method in isolation — they chain them. First, search for an element's position. Then, depending on whether it was found, either insert a new value or remove an existing one.
The three operations work together:
- Search answers "where is it?" (returns an index, or
-1). - Insertion places a new element at a chosen position.
- Removal deletes an element at a given position.
On the AP exam, this combined pattern appears in FRQs about managing rosters, inventories, schedules — anywhere objects come and go based on lookup.
Search first; act second.
The found / not-found result drives the next operation.
Manual sequential search
public int indexOfName(ArrayList<Student> list, String target) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(target)) {
return i;
}
}
return -1;
}
Returns the index of the first match, or -1 when not found. The standard AP CSA contract for search methods.
Insertion at a specific position
list.add(index, newItem); // shifts existing elements right
Common positions: 0 (front), list.size() (back — same as add(item)), or somewhere in the middle based on logic.
Removal at a specific position
list.remove(index); // shifts later elements left
Combined: search-then-remove
public void removeStudent(ArrayList<Student> list, String name) {
int i = indexOfName(list, name);
if (i != -1) {
list.remove(i);
}
}
The if (i != -1) guard is essential. Without it, passing -1 to remove throws an exception.
Combined: search-then-insert-after
public void insertAfter(ArrayList<Student> list, String target, Student newStudent) {
int i = indexOfName(list, target);
if (i != -1) {
list.add(i + 1, newStudent);
}
}
Inserting at i + 1 places the new element directly after the matched one. Edge case: if i + 1 == list.size(), the insertion happens at the end — which add handles naturally.
Combined operation vocabulary.
Used in workflow-style FRQs.
- Search — locating an element by criterion.
- Index of — position of the first match, or
-1. - Insertion at index — placing a new element at a chosen position.
- Removal at index — deleting the element at a chosen position.
- Found / not-found branch — guarding subsequent operations against
-1. - Workflow chain — search, then conditional act, all in one method.
How chained operations are tested.
The not-found guard is the single most common trap.
What FRQ graders check:
- The
i != -1guard. Skipping it crashes when the target is missing. - String equality. Use
.equals(), never==, when searching by name or text. - Correct insertion position.
add(i, x)inserts ati;add(i + 1, x)inserts after. - List integrity after combined operations. Insertion shifts later elements right; removal shifts them left.
MCQ patterns to expect: tracing list state after search-then-modify operations; identifying what happens when the target is absent.
Replace a player in the roster.
Search-then-remove-then-insert in sequence.
Setup. A Player class has getName(). Given a roster, replace the player named oldName with newPlayer at the same position. If oldName isn't found, do nothing.
public void replacePlayer(ArrayList<Player> roster,
String oldName, Player newPlayer) {
int i = -1;
for (int j = 0; j < roster.size(); j++) {
if (roster.get(j).getName().equals(oldName)) {
i = j;
j = roster.size(); // exit early
}
}
if (i != -1) {
roster.set(i, newPlayer);
}
}
Trace. Roster contains [Alex, Ben, Cara]. Call replacePlayer(roster, "Ben", new Player("Drew")):
- Search finds Ben at index 1.
- Guard passes (1 != -1).
set(1, newPlayer)replaces Ben with Drew.
Final roster: [Alex, Drew, Cara]. Size is unchanged because set doesn't shift.
What if oldName were "Zach"? Search returns -1. Guard fails. List untouched. No exception.
Why this matters. The replace-in-place operation uses set (no shifting) instead of remove+insert (two shifts). When the prompt says "replace" at the same position, prefer set.
Combined-operation errors.
Most come from missing the not-found case.
- No guard before
remove(i)oradd(i, x). Ifiis -1, both throw exceptions. - Using
==for String comparison. Always use.equals(). - Wrong insert position.
add(i, x)goes before positioni;add(i + 1, x)goes after. - Searching after modifying. If you remove first, indices shift; searches based on old positions break.
- Returning early without updating. If the prompt asks for a void method, just check the guard and act.
- Confusing
setwith insertion.setreplaces;add(i, x)inserts.
Search + modify patterns.
Pick the right combination for the task.
Insertion / Removal / Search Reference
AP Quick Reference| Goal | Pattern | Guard needed? |
|---|---|---|
| Find an element | Loop + .equals() + return i |
Return -1 at end. |
| Remove found element | Search → if not -1, remove(i) |
Yes. |
| Insert after found element | Search → if not -1, add(i + 1, x) |
Yes. |
| Insert before found element | Search → if not -1, add(i, x) |
Yes. |
| Replace found element | Search → if not -1, set(i, x) |
Yes. |
| Add only if absent | Search → if -1, add(x) |
Yes (inverted). |
How to master combined operations.
Always write the search helper; always guard the action.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each option, trace what happens when the target is present vs missing. Spot any missing not-found guard.
- Java Lab — implement methods that combine search with remove, insert, or replace. Test with both found and missing targets every time.
- FRQ Practice — write a search helper first; then use its result with an
if (i != -1)guard before any modification.
If your reflex is "search, guard, modify", combined-operation FRQs become standard fare.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.