As Eric implies, the length of the list changes as items are removed from it, and so do the indices of all of the values after the element that has just been removed.
I'm not sure what the significance of "lower" is. I did notice that the loop that iterates through "lower" attempts to remove elements from "upper". Is this intentional?
This is my solution based on a "remove" list of points that should be removed from "upper". It is also possible to use the style of your original test except that each == check has to to be replaced by an equals() check.
If the equals(...) implementation is removed from the Point class, nothing will be removed from "upper" because the test case deliberately uses clones of the original a,b,c and d values.
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
public class TestArrayList
{
@Test
public void testRemove()
{
// Test fixture:
Point a = new Point(115, 70);
Point b = new Point(139, 66);
Point c = new Point(195, 111);
Point d = new Point(144, 165);
List<Point> upper = new ArrayList<Point>();
upper.add(a.clone());
upper.add(b.clone());
upper.add(c.clone());
upper.add(d.clone());
List<Point> remove = new ArrayList<Point>();
remove.add(a.clone());
remove.add(b.clone());
remove.add(c.clone());
remove.add(d.clone());
// Assertions:
Assert.assertTrue(upper.size() == 4);
Assert.assertTrue(remove.size() == 4);
// Modified code:
System.out.println(upper.toString());
System.out.println(remove.toString());
for (Point p : remove)
{
upper.remove(p);
}
System.out.println(upper.toString());
System.out.println(remove.toString());
// Assertions:
Assert.assertTrue(upper.isEmpty());
Assert.assertTrue(remove.size() == 4);
}
}
class Point implements Cloneable
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public Point clone()
{
return new Point(x, y);
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
else if (o instanceof Point)
{
Point p = (Point) o;
return x == p.x && y == p.y;
}
else
{
return false;
}
}
@Override public String toString()
{
return "X: " + x + " Y: " + y;
}
}