Of course, as soon as you remove the 0th item, the last item is now 18th, because the items are reindexed.
You can use several tricks, for example, remove starting from the end. Or remove the 0th item until the array is empty (or until you removed some predefined number of items).
Code:
for(int index = mArrayList.size() - 1; removeIndex >= 0; removeIndex--) {
myArrayList.remove(removeIndex);
}
or
for(int nremoved = mArrayList.size() - 1; nremoved >= 0; nremoved--) {
myArrayList.remove(0);
}
If you want to remove all the items, you can consider using clear() as well.
If you want to remove several positions from a list, you can try the following:
Collections.sort(positions); // needed if not already sorted
for (int i = positions.size() - 1; i >= 0; i--)
myArrayList.remove(positions.get(i));