A big use case of iterators with ArrayLists is when you want to remove elements while iterating. You have only three safe solutions :
- use an iterator and its
remove
method
- copy the elements you want to keep in another list
- jungle with indexes
Assuming you don't add
while iterating, using the iterator is a mean to avoid the ConcurrentModificationException
.
The readability argument is subjective. Personally I don't find a cleanly declared iterator less readable. And it doesn't really matter as the iterator is the safe way to iterate and remove at the same time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…