[Solved] Check the code. Don’t understand why logic not work totally [closed]


Not quite clear on your code, but when you remove an element from an ArrayList that you are iterating through, you do not want to increment the index, otherwise you will skip elements, e.g.:

ArrayList<...> list = ...;

for (int i = 0; i < list.size(); ) {
   if (shouldBeRemoved)
       list.remove(i);
   else
       ++ i;
}

In other words, if you remove the element at index i, the next element is now at i, not at i + 1.

Since you are skipping elements, then you may find that some that should have been checked are not, and thus are remaining in the list.

1

solved Check the code. Don’t understand why logic not work totally [closed]