I want to do something like
this
However, I do NOT want the added elements to be repeated. Basically I have a basic arraylist and I am returning an iterator over an arraylist. Repeating the use of this iterator, I want to add elements to the original arraylist. How to do it?
EDIT: The problem is that I need objects in the iterator modified by the iterative code. I don't think that cloning an arraylist would work ...
EDIT2: Here is a stripped-down version of my code.
public class Map {
private ArrayList<Robot> robots;
public Iterator<Robot> getRobots() {
return robots.iterator();
}
public void buildNewRobot(params) {
if(bunchOfConditions)
robots.add(new Robot(otherParams);
}
}
And now the card is used in another class.
for(Iterator<Robot> it = map.iterator(); it.hasNext();){
Robot r = it.next();
if(condition)
map.buildNewRobot(params);
}
source
share