ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
int total = 0;
for (int x : list) {
total += x;
if (x == 2) list.add(99);
}
System.out.println(total);What output is most likely?
- A
6
- B
99
- Ccheck_circle
ConcurrentModificationException
- D
105
Explanation
Modifying an ArrayList during a for-each loop throws ConcurrentModificationException at runtime.