iterator.remove(). is good however my question: if I want while to iterate, for a kind of condition, to delete another object from the array List.
Example (al being the arraylist)
for (Iterator i = al.iterator(); i.hasNext(); ){
IEvent event =(IEvent) i.next();
if (nbSendingWTS > 0 || nbSendingCTS > 0){
i.remove();
al.remove(swtsee);
al.remove(sdctsee);
System.out.println("dropping evtg");
}
This is giving me an error: Exception in thread "main" java.util.ConcurrentModificationException
Also the normal iteration:
for(IEVEnt event:al){}
is giving an error
To be more clear the swtsee a d sdctsee are taken from previous iterations on the arraylist and saved so i can delete if I have the new condition. So is there a way when I detect them to shift them to higher indexes and then I use a reverse iteration?
What to do?
ArrayBlockingQueue,ConcurrentLinkedQueue, or other collections designed for concurrent use. Their iterators are guaranteed not to throw this exception. But they're probably overkill unless you're really using multiple threads.