Traversing ArrayLists

AP Computer Science A· difficulty 4/5

for (Integer x : list) {
    if (x == 0) list.remove(x);
}

What problem occurs?

  • A

    for-each cannot iterate ArrayList

  • B

    list.remove takes an int index, not an Integer

  • C

    Nothing happens

  • D

    ConcurrentModificationException because the list is modified during a for-each

    check_circle

Explanation

Modifying a collection while iterating with an enhanced for-loop triggers ConcurrentModificationException. Use an explicit Iterator or an index-based loop.

Want 10 more like this — adaptive to your weak spots?

Related questions