Check out the loop where you delete items again. Every time you delete an element, the value returned by al.size () decreases, and the index increases. This means that you will only carry out half the time when you want.
The correction will be performed.
int size = al.size(); for(int index = 0; index < size; index++ ) {
then work. thus the size does not change.
Another thing to keep in mind is that when you delete something from an arraylist with index 0, index 1 will become index 0. So it would be better to iterate with
int index = al.size(); index >=0 ; index--
source share