Java – delete elements from copyonwritearraylist

I received an exception when I tried to delete an element from copyonwritearraylist using an iterator

(from http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html )

Now, surprisingly, I can iterate with foreach and use the remove () function But when I try to delete an item from the list using the for loop, I get a famous error - you skip the element next to the deleted element So what do you suggest

Solution

Iterate over the collection, select all elements to delete, and place them in the temporary collection After the iteration, use the removeAll method to delete all found elements from the original collection

Will this work for you? I mean, I don't know if the deletion logic is more complex than your algorithm

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>