FIFO removal: taking from the front.
When the first one in is the first one out — a queue, a print job, a customer line — repeatedly removing index 0 is the pattern.
FIFO stands for First In, First Out. The earliest-added element is also the next to be removed. ArrayList supports this directly with remove(0), which removes and returns the element at the front.
Whenever you process items in arrival order — print queues, ticket dispensers, simulation steps — you're using FIFO. The pattern is simple: while the list isn't empty, remove from the front and process.
On the AP exam, FIFO patterns appear in tracing problems and in FRQs that involve "process the next" semantics. Knowing the cost of shifting also helps you reason about efficiency.
Remove from index 0; remaining elements shift left.
Every front removal moves the rest down by one position.
The FIFO template
while (!queue.isEmpty()) {
SomeType next = queue.remove(0);
process(next);
}
On the AP CSA exam, you can also write queue.size() > 0 instead of !queue.isEmpty().
What remove(0) does
It removes the element at index 0 and returns it. Every other element shifts down by one position: what was at index 1 becomes index 0, what was at index 2 becomes index 1, and so on. The list's size decreases by one.
Adding to the back
queue.add(newItem); // appends to the end
Combined with remove(0), you get classic queue behavior: add at the back, remove from the front.
Why shifting costs matter (conceptually)
Each remove(0) requires moving every remaining element down by one. For a list of size n, that's about n operations. Doing this repeatedly on a large list is slow — but the AP exam tests correctness, not optimization. For AP problems, remove(0) is the standard FIFO tool.
Processing example
public void printAllJobs(ArrayList<String> queue) {
while (!queue.isEmpty()) {
String job = queue.remove(0);
System.out.println("Printing: " + job);
}
}
After the method finishes, the queue is empty. Every job has been printed in arrival order.
Queue terminology.
Used in any FIFO-style problem.
- FIFO — First In, First Out — earliest added, earliest removed.
- Queue — a collection that follows FIFO order.
- Front / head — index 0; the next element to be removed.
- Back / tail — the last index; where new elements are added.
- Enqueue — add to the back (
add). - Dequeue — remove from the front (
remove(0)). - Drain — process every element until the queue is empty.
How FIFO patterns are tested.
Loop termination and processing order are the main check points.
MCQ patterns to expect:
- "What is printed?" Trace removals from index 0 in order.
- "What is the size after k removals?" Start size minus k, but not below 0.
- "Why does this loop terminate?" Because each iteration removes an element; size shrinks toward 0.
FRQ tip: when the prompt says "process in the order received" or describes a queue, reach for the while (!list.isEmpty()) + remove(0) pattern.
Process a ticket queue.
Trace every removal and the resulting size.
Problem. Given ArrayList<String> tickets containing ["A", "B", "C", "D"], what is printed?
while (!tickets.isEmpty()) {
String t = tickets.remove(0);
System.out.println(t);
}
Trace.
- Iteration 1:
remove(0)returns"A". PrintA. Queue:["B", "C", "D"]. - Iteration 2:
remove(0)returns"B". PrintB. Queue:["C", "D"]. - Iteration 3:
remove(0)returns"C". PrintC. Queue:["D"]. - Iteration 4:
remove(0)returns"D". PrintD. Queue:[]. - Loop check: queue is empty → exit.
Output. A B C D (one per line), in arrival order.
Final state. The queue is empty; size() == 0.
Why this matters. Notice how every iteration shifted the remaining elements. The whole point of FIFO is that the order of processing matches the order of insertion — which the leftward shift naturally preserves.
FIFO pitfalls.
Most come from confusing the loop or the index.
- Using an indexed
forloop withremove(0). The size changes every iteration, and the index never increments to a stable position. Use awhileloop instead. - Using
remove(list.size() - 1)for FIFO. That's LIFO (stack-like), not FIFO. - Forgetting to capture the return value.
remove(0)returns the removed element; use it. - Calling
remove(0)on an empty list. Throws an exception. Always guard with an emptiness check. - Reading
list.get(0)before removing and then incrementing your own counter — completely unnecessary, just useremove(0).
FIFO at a glance.
The operations and their guarantees.
FIFO Reference
AP Quick Reference| Operation | Method | AP Exam Tip |
|---|---|---|
| Enqueue (add to back) | list.add(item) |
Appends at the end. |
| Dequeue (remove from front) | list.remove(0) |
Shifts remaining elements left. |
| Loop condition | while (!list.isEmpty()) |
Or list.size() > 0. |
| Loop type | while, not for |
Size changes every iteration. |
| Empty check | Before any remove(0) |
Prevents runtime exception. |
How to master FIFO removal.
Trace each removal until the queue empties.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each problem, write the queue contents after every iteration. The first removed element is always the originally-first element.
- Java Lab — implement a print-queue simulator: add several jobs, drain them with
remove(0), verify FIFO order on the output. - FRQ Practice — when the prompt describes a queue, customer line, or arrival order, use
while (!list.isEmpty())withremove(0).
If you can spot the FIFO pattern in a prompt within five seconds, you have the right reflex for any queue-style problem.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.