for
for(Integer item : new ArrayList<Integer>(myCollection)){
myCollection.add(first.intValue() + item.intValue());
}
( )
for (Iterator<Integer> iterator = new ArrayList<Integer>(myCollection).iterator();
it.hasNext(); ) {
Integer item = iterator.next();
myCollection.add(first.intValue() + item.intValue());
}
myCollection ( myCollection - List), ( )
for(int ctr = 0; ctr < myCollection.size(); ctr++){
Integer temp = myCollection.get(i);
myCollection.add(first.intValue() + temp.intValue());
}
... but you are changing myCollectioninside the loop, so this second loop will never reach the end (assuming there is at least one element in it).
This way your ArrayList helps keep your loop well.
source
share