I have two arrays: Walls and Neighbors.
public boolean[] walls = new boolean[4];
public Cell[] neighbors = new Cell[4];
and I have an Enum:
i strongly recommend against it because the Ordinal value is based on the order in your java code.
The use of ordinal() will make you code very hard to maintain, especially if some form of persistence enters the equation.
for example if you decide to add diagonal directions like NORTH_WEST, SOUTH_EAST. if you are using ordinal(), you must add these at the bottom of your list or else what used to be SOUTH might become NORTH.
ie you can change your enyum to without (possibly) changing functionality
N (is now 0 was 0)
NE (is now 1)
E (is now 2 was 1)
SE (is now 3 )
S (is mow 4 was 2)
SW (is now 5)
W (is now 6 was 3)
On a tangential issue, it might be better to use an EnumMap for your neighbours:
Map<Dir, Cell> neighbours =
Collections.synchronizedMap(new EnumMap<Dir, Cell>(Dir.class));
neighbours.put(Dir.North, new Cell());
for (Map.Entry<Dir, Cell> neighbour : neighbours.entrySet()) {
if (neighbour.isVisited()) { ... }
}
etc..
BTW: Enum instances should by convention be all caps,
enum Dir {
NORTH,
EAST,
SOUTH,
WEST
}
If you are not persisting the arrays or in any other way are making yourself dependent on different versions of your enum class, it's safe to use ordinal()
.
If you want don't want to rely on the implicit ordering of the enum values, you could introduce a private index value:
public enum Direction {
NORTH(0),
SOUTH(1),
EAST(2),
WEST(3);
private int _index;
private Direction (int index_)
{
_index = index_;
}
public int getIndex()
{
return _index;
}
}
From here it's easy to both allow for easy lookup of index to Direction (By creating a Map in a static block for compact persistence; Do uniqueness check in static block etc.
You can also enhance an enum (index clockwise):
enum Dir
{
NORTH(0),
SOUTH(2),
EAST(1),
WEST(3);
private final int index;
private Dir(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
}
Using an enum's ordinal for your use depends on implicit order. I like to be explicit especially if you use the integer values as index into your array linking value to meaning.
In this case I would therefore opt to use final static int NORTH = 0
, etc.
The documentation only says that most programmers will have no use for the method. This is one case of legitimate use. Assuming your class controls both the enum and the array, there is no reason to fear the ordinal()
method for indexing the array (since you can always keep them in sync).
However, if your usage gets any more complicated, you will likely want to use an EnumMap instead, as has been suggested.