Using the iterator over the same set more than once in java

后端 未结 6 1032
耶瑟儿~
耶瑟儿~ 2021-01-13 20:08

Say I\'m iterating over a set in java, e.g.

Iterator<_someObject_> it = _someObject_.iterator();

well, first I want to go through eve

6条回答
  •  不思量自难忘°
    2021-01-13 20:35

    Sorry, but the contract of an Iterator states that you can only go through it once.

    Given that you can't know for sure the order of the next Iterator, or even the content for that matter (it might change!), you're better off doing all the modifications or method calls in the same block.

提交回复
热议问题