I tried to reproduce ConcurrentModificationException myself by writing the following code:
List<String> last = new ArrayList<>();
last.add("a");
last.add("b");
for(String i : last){
System.out.println(i);
last.remove(i);
}
System.out.println(last);
Since, the documentation of ArrayList
mentioned
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.
I expected that in single-threaded programs such detection is straghtforward. But the program printed
a
[b]
instead. Why?