How to remove specific element on List?
...
java.util.List<Polygon> triangles = new LinkedList<Polygon>();
Point startDrag, endDrag, midPoint;
Polygon triangle;
....
int[] xs = { startDrag.x, endDrag.x, midPoint.x };
int[] ys = { startDrag.y, startDrag.y, midPoint.y };
triangles.add( new Polygon(xs, ys,3));
....
public void mouseClicked(MouseEvent e) {
...
startDrag = new Point(e.getX(), e.getY());
for (Polygon p:triangles){
if (p.contains(startDrag)) //Polygon has a 'contains(Point)' method
remove (p.contains(startDrag));
}
....
You will not be able to remove the object from the
triangleslist if you are currently iterating over it. If you try to do so, you will trigger aConcurrentModificationException. Instead, what you should do is make a copy of the list and iterate over that, and when you get a hit, remove the item from the original: