I am trying to iterate through an ArrayList
and remove the actual object (index s) under certain conditions (x == 0
). It always gives an error at the line where the object should be removed, if executed. Without the remove()
it runs perfectly fine.
int s = 0;
int x = 0;
if (!objectList.isEmpty()) {
for (obj actualObj : objectList) {
if (x == 0) {
objectList.remove(s);
} else {
System.out.println("x != 0");
}
s++;
}
} else {
System.out.println("list is empty");
}