Looking at the posX
and posY
, I'm curious if something like ArrayList<Point>
is a better solution for you.
The reason the remove
couldn't find the array is because the new array is not equals
to the array already in the collection.
(new int[0]).equals(new int[0]) // false!
If you create you own Point
class, then you can @Override equals
to behave as you want, and you can simply call remove(new Point(posX, posY))
.
You should also consider having a Set<Point> positionList
instead, because implementations offer much faster removal (O(1)
for HashSet
, O(log N)
for TreeSet
). Remember to @Override hashCode
(which you have to do anyway if you @Override equals
), and make Point implements Comparable<Point>
(or provide an external Comparator<Point>
) if you want to use TreeSet
or need to sort the points in other contexts.
If your int[]
has many elements and a custom Point
class is not applicable, then you may want to consider switching to List<Integer>
instead (see also: Effective Java 2nd Edition, item 25: prefer lists to arrays). It has the equals
behavior that you need. It is slower, but it may still be fast enough.
Lastly, if you insist on using int[]
, you can just wrap it inside your own IntArray
class, and have a ArrayList<IntArray>
instead. @Override equals
and hashCode
to use Arrays.equals(int[], int[])
, and hashCode(int[])
respectively.