iterator iterator and for loop

iterator iterator:

Iterator supports safe removal of objects from the source collection by simply calling remove() on the Iterator . The advantage of this is to avoid the ConcurrentModifiedException, which is exactly what the name implies: when the Iterator is opened to iterate over the collection, the collection is being modified at the same time. Some collections do not allow removing or adding elements while iterating, but it is safe to call the Iterator 's remove() method. If the collection is added or deleted during the iteration, it will cause the iterator to report an error.

for loop:

If the collection is added during the for loop, it will not affect the for loop.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481623&siteId=291194637