Removing items from a collection in java while iterating over it

后端 未结 10 1768

I want to be able to remove multiple elements from a set while I am iterating over it. Initially I hoped that iterators were smart enough for the naive solution below to wor

10条回答
  •  粉色の甜心
    2020-11-28 09:13

    Copied from the Java API:

    The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

    I thought I would point out that the ListIterator which is a special kind of Iterator is built for replacement.

提交回复
热议问题