Hello! An exception appears in this piece of code . moreover, after the 1st method call from return. It seems that the collection gets a new iterator and works with it, but something goes wrong.
Closed due to the fact that off-topic participants fori1ton , Alexey Shimansky , Kromster , ermak0ff , user194374 16 Feb '17 at 5:58 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - fori1ton, Kromster, Community Spirit
- complete the question with a sample code. You do not need to publish it on third-party resources, the site allows you to do it on it. - Artem Konovalov
- onePossible duplicate issue: ConcurrentModificationException - Alexey Shimansky
|
1 answer
You remove or modify items by iterating through the collection. Use an alternate pass method instead of an iterator (for example, a simple loop other than for each ).
You cannot use Iterator and, at the same time, add or remove items from the collection. ConcurrentModificationException is the same and says that you are simultaneously modifying the collection.
A similar error will occur if you use the for each loop, changing the elements in it. For this, either do while is suitable, or simple for .
- Yes, thanks, redid. But where was the removal? With waves = tempWaves? But after all, the iterator was always new - Oleg Rosaybeda
- @OlegRozibeda, I apologize, did not fully write. Adding, by meaning, leads to a similar result. I came across only deletion, so I wrote about it :). - Rostislav Dugin
- Yes, I know that, but where was the addition? I'm adding all the new objects to a completely different collection, not the one from which I got an iterator - Oleg Rozdaybeda
- @ OlegRazdaybeda, in this piece of code - I do not see. But outside of the method, it is modified somewhere. Search for matches by
iter.. - Rostislav Dugin - no, outside of this piece of code, iter is no longer used - Oleg Rozdaybeda
|