TOPIC 4.9 · STUDY
ArrayList Traversals
An indexed loop walks an ArrayList with i from 0 through size() - 1 and uses get(i) or set(i, value). An enhanced for loop visits each element in order. When an indexed loop removes, it must not skip the item that slides into the current index.
4.9.A
ArrayList Traversals
Traversing an ArrayList means visiting its elements in a planned order. Legal indexes run from 0 through size() - 1. Calling get or set with size() itself is out of range and throws IndexOutOfBoundsException.
An indexed for loop is the usual tool when you need the index. Start i at 0, keep going while i is less than size(), and use get(i) to read or set(i, value) to replace the element at that slot. After each body run, i increases by 1 so the next legal index is next.
ArrayList<String> trays = new ArrayList<String>();
trays.add("rye");
trays.add("oat");
trays.add("seed");
for (int i = 0; i < trays.size(); i++) {
System.out.print(trays.get(i));
}
trays.set(1, "bran");
System.out.print(trays.get(1));
The loop prints ryeoatseed because i takes 0, then 1, then 2. After set(1, "bran"), get(1) prints bran. The test must be i < trays.size(), not i <= trays.size(), or the last trip would call get on an illegal index.
Indexes sit under the cells. With size() equal to 3, a full forward pass uses get(0), get(1), and get(2).
An enhanced for loop visits each element without naming an index. Write for (Type name : list) and use name inside the body. The loop still walks from the first stored value to the last, so printing every element matches a careful indexed loop that only calls get.
ArrayList<Integer> miles = new ArrayList<Integer>();
miles.add(3);
miles.add(7);
miles.add(2);
for (Integer m : miles) {
System.out.print(m);
}
That fragment prints 372. Enhanced for is a good fit when you only need the value. Prefer an indexed loop when you must call set, remove, or otherwise use the index.
Removing inside a forward indexed loop can skip the next item. After remove(i), later elements slide left into lower indexes, but the for header still does i++. The value that just slid into index i is never examined unless you decrease i after the remove, or you traverse from the back so a remove cannot hide an unvisited neighbor.
ArrayList<Integer> packs = new ArrayList<Integer>();
packs.add(5);
packs.add(0);
packs.add(0);
packs.add(4);
for (int i = 0; i < packs.size(); i++) {
if (packs.get(i) == 0) {
packs.remove(i);
i--;
}
}
System.out.print(packs);
Without i--, removing the first 0 leaves [5, 0, 4] and the next i++ jumps to the 4, so a 0 remains. With i--, both zeros are deleted and the print shows [5, 4]. Do not add or remove inside an enhanced for loop; changing the list size there can throw ConcurrentModificationException.
Key points
- Legal indexes for get and set run from 0 through size() - 1.
- An indexed loop uses i from 0 while i < size() and calls get(i) or set(i, value).
- An enhanced for loop visits each element in order without an index.
- An indexed loop that removes must not skip the next item; use i-- after remove or walk backward.
- Do not add or remove during an enhanced for loop.
The loop bound is size(), and size changes if you add or remove. Recheck size() each time (or adjust the index) so the loop still matches the list that is left.