Closed PedroMPagani closed 3 years ago
Concurrent modification (i.e. modification while iterating) is not allowed, fastutil does not exhaustively try to detect such modification for performance reasons. Both loops are invalid in that sense. Any modification to a collection effectively invalidates any existing iterators (except if the modification is performed through the iterator).
Instead of s.remove(g.nextIndex())
you could use something like g.remove()
(I think this is how it is called).
To remove all objects, use s.clear()
. If you want to select which to remove, use s.removeIf(...)
or make a copy of the list.
For further help, search for ConcurrentModificationException
and how to avoid it (this is what most java collections would directly throw for this kind of code).
While using g.remove() it gives me an error,
Exception in thread "main" java.lang.IllegalStateException
at it.unimi.dsi.fastutil.objects.ObjectArrayList$1.remove(ObjectArrayList.java:895)
at com.styp.totem.manager.STRank.
You also need to call g.next()
after removing (or potentially before it, I am not entirely sure!). .remove()
does not advance the iterator.
So something like
while (g.hasNext()) {
g.next();
g.remove();
}
Sorry for the dumb act, thanks for the support. ( I think its before.)
Sorry for the dumb act, thanks for the support. ( I think its before.)
No problem 😄 Happens to everyone.
Alright, i have this code and when u run it, it removes the /2 of the list size, not it all, when i remove using the iterator and nextIndex() works fine, but when i try it inside of the foreach it does not work, and it runs only the half of the list on the foreach either, not it all. ` ObjectList s = new ObjectArrayList();