This is a simple code but sometimes the results is not as expected.
I have an ArrayList containing Integers and some elements are duplicates. I wanted to remove all occurrence of the duplicate ones for a certain_val. Most time it worked good and removed all. But sometimes it may miss one and did not remove all the occurrence. The code is as following:
for (int i = 0; i < arraylist.size(); i++) {
if (arraylist.get(i) == certain_val){
arraylist.remove(i);
}
}
The IntelliJ does have a warning on the .remove line, saying “Suspicious ‘list.remove()’ in loop”.
I don’t know the exact issue with this problem.