I am having problems with the logic of using an iterator. I need to remove elements from an ArrayList inside a loop, so I decided to use an Iterator object. However, I'm not sure how I can convert the source code to an iterator.
Original loop:
ArrayList<Entity> actorsOnLocation = loc.getActors();
int size = actorsOnLocation.size();
if (size > 1) {
for(Entity actor: actorsOnLocation) {
for(int nextEnt=index+1; nextEnt < size-1; nextEnt++) {
Entity opponent = actorsOnLocation.get(nextEnt);
}
}
}
I understand that I will have to use while-loops, which will work for the first loop. But how can you convert the conditions of the second loop into what works with Iterator? This post says that you can get an iterator at any time, but in the documentation I can not find anything like a method .get.
ArrayList<Entity> actorsOnLocation = loc.getActors();
int size = actorsOnLocation.size();
Iterator actorsIterator = actorsOnLocation.iterator();
if (size > 1) {
while(actorsIterator.hasNext()) {
Entity actor = (Entity) actorsIterator.next();
int index = actorsIterator.indexOf(actor);
for(int nextEnt=index+1; nextEnt < size-1; nextEnt++) {
Entity opponent = actorsOnLocation.get(nextEnt);
}
}
}
: , , ? , , , ?
, , , . , ?