LinkedList iterator remove [duplicate]

跟風遠走 提交于 2019-12-01 18:11:17
Reimeus

You cannot remove an item from the collection directly while iterating through the elements as this will cause a ConcurrentModificationException. Iterator.remove() is the accepted safe way to modify a collection during iteration. To avoid seeing an IllegalStateException, make sure to call Iterator.next():

while (itr.hasNext()) {
   itr.next();
   itr.remove();
}

or as you simply wish to remove all elements in the Collection, you could use:

flights.clear();

See: Efficient equivalent for removing elements while iterating the Collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!