$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #259

Which fix removes the bug?

for (String n : names) {
    if (n.isEmpty()) names.remove(n);
} // ConcurrentModificationException
  1. Mark the list field volatile
  2. Wrap it in synchronized (names)
  3. Use names.removeIf(String::isEmpty)
  4. Use names.remove(names.indexOf(n))
Changing an ArrayList structurally during for-each breaks its iterator, even in one thread. removeIf (or Iterator.remove) removes safely.
Play Problem Solving ▸