0

I have a for-loop to iterate over my population. For a condition, I want to remove that object from my population. When I run my model, a ConcurrentModificationException occurs. The reason should be, that I cannot modify my population, while iterating over the collection in a for-loop.

Is there any simple way to fix that problem? I tried using an iterator, but because my code is in an event, which occurs every minute new, it did not really work.

for (testpop t : testpops) {
    if (dateToTime(t.arrival) <= time()) {
        System.out.println(t.arrival);
        remove_testpops(t);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Steffen
  • 41
  • 5
  • 1
    Loop over a copy of `testpops` (e.g. `for testpop t : new ArrayList(testpops)) ...`, or use an iterator to iterate and use its remove method. I'm not sure why _"because my code is in an event, which occurs every minute new"_ would be a reason that an iterator wouldn't work. – Mark Rotteveel Jan 08 '23 at 12:01
  • you should probably use an iterator which offers remove. – Stéphane Jeandeaux Jan 08 '23 at 12:19

0 Answers0