I'm working on a project for school and i'm trying to create a map that uses an array of size 2 as the index for the map. I'm not even sure if this is possible since I don't know I could access the elements of the map (since I really don't know how i could reference an entire array by value). Basically i'm trying to use the map index as a coordinant system to strings. If anyone could let me know if this is even possible and if it is what the syntax would be that would be a great help. Thanks! I'm doing this prject in c++
If using Java, one approach you could use is to wrap your array with a class, and then implement the hashCode and equals method. These methods are a mechanism which allow other objects to identify an instance of that class. For example, the Map class uses hashCode as the key to store and retrieve that object.
Here's an example of your wrapper class.
class Point {
private int[] coordinates;
public Point(int x, int y){
this.coordinates = new int[]{x, y};
}
@Override
public boolean equals(Object o){
// implement equals as stated in the docs.
}
@Override
public int hashCode(){
// implement hashCode as stated in the docs using coordinates[0] and coordinates[1]
}
}
class App {
public static void main(String[] args){
Map<Point, String> map = new HashMap<Point, String>();
map.put(new Point(1,2), "some string");
// etc...
}
}