Suppose I want to define an enum pointing to an array of objects. Can this be done? In the below code, the commented out part defines an enum pointing to single objects and works fine, but the uncommented code which tries to initialize arrays of objects causes a bunch of errors.
public enum SightSensor{
/*NORTH (new MapLocation(0,1)),
SOUTH (new MapLocation(0,-1));
private final MapLocation loc;
SightSensor(MapLocation loc){
this.loc = loc;
}
public MapLocation getLoc(){
return loc;
}*/
NORTH ({new MapLocation(0,1), new MapLocation(0,2)}),
SOUTH ({new MapLocation(0,-1), new MapLocation(0,-2)});
private final MapLocation[] locs;
SightSensor(MapLocation[] locs){
this.locs = locs;
}
public MapLocation[] getLocs(){
return locs;
}
}
On a related note, suppose I wanted to define a fixed mapping between two enums: what’s the best way to do so? Map? Hash? To be precise, suppose analogously to the above code I wanted to define an object A that that if NORTH is a different enum value A[NORTH] will return the desired list of MapLocations.
I think it should be this:
The bare
{...}construct cannot be used in that context.