Your grid of 100×100 is relatively small, so a 2D array is the way to go.
If your grid is large and sparsely populated, a Map<Point, MyObject>
is a better solution, where Point
has x
and y
coordinates. This will still allow very fast lookup of the object, without having to scan all objects sequentially.
If you implement your own Point
class, remember to implement equals()
and hashCode()
, e.g. something like this:
public final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
@Override
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
@Override
public int hashCode() {
return this.x * 127 + this.y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point that = (Point) obj;
return (this.x == that.x && this.y == that.y);
}
return false;
}
}