开发者

Creating a new iterator object every time I traverse the list

开发者 https://www.devze.com 2023-03-26 00:53 出处:网络
If I create an Iterator object on any List, do I have to create a new iterator object every time I traverse the list? Or do new items added or removed from the list automatically get accounted for by

If I create an Iterator object on any List, do I have to create a new iterator object every time I traverse the list? Or do new items added or removed from the list automatically get accounted for by the previously created i开发者_开发问答terator?


Iterator is a one-use utility. You generally need a new one each time you iterate through the list. However, that's not a bad thing; Iterators have very little state (for an ArrayList, it might be just an int to hold the current index).

If the backing list is modified in between iterator calls, you will get a ConcurrentModificationException from the iterator operation. Adding or removing items must be done using the Iterator itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消