Why?
List<String> shoppingList = new ArrayList<String>(
Arrays.asList(new String[] {
"apple", "banana", "pineapple", "orange"
}));
for (String item : shoppingList) {
System.out.println(item);
}
Iterator<String> iter = shoppingList.iterator();
while (iter.hasNext()) {
String item = iter.next();
System.out.println(item);
}
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
if number == 3 or number == 4:
numbers.remove(number)
print(numbers)
List<Integer> numbers = new ArrayList<Integer>(
Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9}
));
for (Integer number : numbers) {
if (number.equals(3) || number.equals(4)) {
numbers.remove(number);
}
}
System.out.println(numbers);
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997)
at dungeonmania.DungeonManiaController.main(IterExample.java:120)
def shopping_list():
yield 'apple'
yield 'orange'
yield 'banana'
yield 'pineapple'
for item in shopping_list():
print(item)