Which fix removes the bug?
for (String n : names) {
if (n.isEmpty()) names.remove(n);
} // ConcurrentModificationException
- Mark the list field
volatile - Wrap it in
synchronized (names) - Use
names.removeIf(String::isEmpty) - 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 ▸