Challenge 2: ConcurrentModificationException and the Iterator Fix — Possible Solution ==================================================================== Broken attempt — RemoveDemo.java: import java.util.ArrayList; import java.util.List; public class RemoveDemo { public static void main(String[] args) { List names = new ArrayList<>(List.of("Alice", "Bob", "Carol", "Bob")); for (String name : names) { if (name.equals("Bob")) { names.remove(name); // removing directly on the list mid-for-each } } } } Representative runtime exception: Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(...) at java.base/java.util.ArrayList$Itr.next(...) at RemoveDemo.main(RemoveDemo.java:8) Fixed — RemoveDemo.java: import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class RemoveDemo { public static void main(String[] args) { List names = new ArrayList<>(List.of("Alice", "Bob", "Carol", "Bob")); Iterator it = names.iterator(); while (it.hasNext()) { String name = it.next(); if (name.equals("Bob")) { it.remove(); // safe -- the iterator itself performs the removal } } System.out.println(names); } } Output: [Alice, Carol] Explanation: The for-each loop uses a hidden Iterator internally. Calling names.remove(...) directly changes the list's structure without that hidden iterator knowing, so its next call to next() detects the mismatch and throws. Using the Iterator's own remove() keeps the iterator's internal state in sync with the list, since the iterator itself is the one performing the removal. WHY THIS WORKS AS AN ANSWER ------------------------------ This reproduces the exact ConcurrentModificationException the chapter's warn-box describes, then applies the chapter's own stated fix -- Iterator.remove() -- as the only safe way to remove elements during iteration.