My requirement is to remove from ArrayList like this:
ArrayList<User> user = new ArrayList<User>();
ListIterator<User> outerIterator = null;
ListIterator<User> innerIterator = null;
User outer = null;
User inner = null;
for(outerIterator = user.listIterator(); outerIterator.hasNext();) {
outer = outerIterator.next();
for(innerIterator = user.listIterator(); innerIterator.hasNext();) {
inner = innerIterator.next();
if(SomeOperationOn(outer,inner)) {
innerIterator.remove();
}
}
}
Above code is giving exception
Exception in thread "main" java.util.ConcurrentModificationException
as expected, because I am trying to remove from innerIterator while outerIterator is Iterator on same object(user).
Is there any way to remove element from ArrayList using ListIterator in loop inside loop?
ConcurrentModificationException, because of the call toouterIterator.remove(), yourArrayListwill be empty at the end of this code. Is this what you want?