For example:
I have an ArrayList
composed of Points. Every point have an array of ints which defines his position. I can have few points in this same position and add them to my ArrayList at every index I want.
The question is:
I have given position and I want to check if any point of my ArrayList have this position. If yes, i return it. If there are few points with this position i need to return the last added (by time). Method equals won't work. I need to compare it with ==, so i need the correct the reference to the newest point.
How i should do it?
I should build another list of unique points, if in this list is another point with the same position I should replace it with newer?
public class Point {
private final int DIMENSIONS;
private final int[] position;
public Point(int dimensions) {
DIMENSIONS = dimensions;
position = new int[DIMENSIONS];
}
public void setPosition(int dim, int value) {
position[dim] = value;
}
public int getPosition(int dim) {
return position[dim];
}
}