Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which one of the following statements about the only way to remove elements safely while iterating a collection is true?

  • A.Call remove on the Iterator itself.
  • B.Call remove on the collection.
  • C.Collect the doomed elements first.
  • D.Iterate backwards.

Answer: A

The iterator's own remove is the documented exception to fail-fast checking; every other structural change made while an iterator is live invalidates it. Removing through the collection during a for-each loop is exactly the case that raises ConcurrentModificationException.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.