List<Integer> listArr = new ArrayList<>();
listArr.add(5);
listArr.add(7);
listArr.add(90);
listArr.add(11);
listArr.add(55);
listArr.add(60);
for(int i = 0; i < listArr.size(); i++) {
if (listArr.get(i) % 2 != 0) {
listArr.remove(i);
}
}
I'm trying to remove all odd numbers from the ArrayList and it must be for or foreEach loop. The result would be 7, 90, 55, 60 for the numbers that are remaining in the ArrayList after loop is finished. When I set a condition:
if(listArr.get(i) % 2 == 0)
Everything is working fine. All even numbers are removed, but in the first example that's not the case for odd numbers. Why is this happening?