Hi
I have written this code that with output you can get that .remove() method doesn’t work. a, b, c, and d are some Points Objects that have x and y members.
Here are a and b and c and d values, which in the if statement must be deleted for upper but it doesn’t.
X :59 Y: 143
X :165 Y: 140
X :59 Y: 143
X :165 Y: 140
System.out.println(upper.toString());
for(int i =0;i<upper.size();i++)
if(upper.get(i)==a||upper.get(i)==b||upper.get(i)==c||upper.get(i)==d){
upper.remove(i);
}
for(int i =0;i<lower.size();i++)
if(lower.get(i)==a||lower.get(i)==b||lower.get(i)==c||lower.get(i)==d){
upper.remove(i);
}
System.out.println(upper.toString());
System.out.println(lower.toString());
first println : [X :108 Y: 89, X :165 Y: 140]
second println: [X :108 Y: 89, X :165 Y: 140]
third println : [X :105 Y: 191]
If I’m reading your question right, you’re assuming that
==will compare the properties of two objects. It doesn’t, that’s whatequalsdoes.==tells you whether two references are to the same object instance, not to equivalent ones.So for example:
Separately: Consider what happens when you have two consequtive matching objects in the
ArrayListthat you have to remove. Say they’re at indexes 8 and 9 in the list. So wheni == 8, you remove the item at index8, and the one that used to be at9is now at8. But then you incrementiin the for loop and continue with the new item at index9, leaving the second one untouched. If you want to modify the list while you’re looping through it, consider looping backward to avoid that, or using anIterator.