dev only

ArrayList Traversals

Same Loop Shapes as Arrays

For-each, indexed for, and while all work on an ArrayList — just with get(i) instead of [i], and .size() instead of .length:

for (String log : logs) { System.out.println(log); }             // for-each — read-only pass
for (int i = 0; i < logs.size(); i++) { System.out.println(logs.get(i)); } // indexed

The Big Restriction: Don't Resize During a For-Each

Adding or removing elements from an ArrayList while a for-each loop is traversing it throws ConcurrentModificationException. If you need to add or remove while looping, use an indexed for or while loop instead.

Removing While Looping — The Careful Way

Removing an element shifts every later element down by one index. If you increment the loop index unconditionally after a removal, you skip checking the element that just got shifted into the spot you were on:

int i = 0;
while (i < logs.size())
{
    if (shouldRemove(logs.get(i)))
    {
        logs.remove(i); // don't increment i here — the next element just shifted into index i
    }
    else
    {
        i++; // only advance when nothing was removed this iteration
    }
}

Chaining Through a List of Objects

ArrayList<Subsystem> subsystems = new ArrayList<Subsystem>();
String firstName = subsystems.get(0).getName(); // chain: get the object, then call a method on it

Common Pitfalls

  • Adding/removing inside a for-each loop. Throws ConcurrentModificationException — switch to an indexed for or while loop if the list's size needs to change mid-traversal.
  • Incrementing the index unconditionally after a remove. This skips the element that shifted into the just-vacated slot — only advance the index when you didn't remove anything that pass.
  • Using <= against .size(). Same off-by-one risk as arrays — the last valid index is size() - 1.

Key Takeaways

  • ArrayList supports the same loop shapes as arrays, using get(i)/.size() instead of [i]/.length.
  • Never add or remove elements from an ArrayList while traversing it with a for-each loop.
  • When removing elements in an indexed/while loop, only advance the index on iterations where nothing was removed.

Derived from csawesome-2026: unit-4-data-collections/4.09-arraylist-traversals.md (CSA 4.9)