You are creating a new ArrayList
and adding elements to be removed from 1st list to this list. But you must understand when you define a array
or List
you merely define or declares references of that type. Since you are adding elements of first List to the other(shallow copy), both Lists will have elements pointing to same instance internally. So no matter if you iterate on one and remove from other same internal Collection
gets modified and hence you get the ConcurrentModificationException
.
So now you can either create Iterator
of the List
, iterate and remove or you can do
battleObjects.removeAll(remove);