Is it bad practice to use an Enum's ordinal value to index an array in Java?

Viewed 24338

I have two arrays: Walls and Neighbors.

public boolean[] walls = new boolean[4];
public Cell[] neighbors = new Cell[4];

and I have an Enum:

enum Dir
{
    North,
    South,
    East,
    West
}

Now, I would like to be able to access walls or neighbors by their direction, so I don't have to pass around a bunch of magic indexes.

However, when I was reading the documentation for Enum.ordinal() it said that programmers will have almost no use for this method which made me think it shouldn't be used in this way.

I was thinking of doing something like:

    List<Dir> availableDirections = new ArrayList<Dir>();
    for(Dir direction : Dir.values())
        if (!Neighbors[direction.ordinal()].Visited)
            availableDirections.add(direction);

or even:

return Neighbors[Dir.North.ordinal()];

Should I revert to using static constants for NORTH, SOUTH, EAST, WEST with the index value set to them or use an Enum's ordinal method?

7 Answers
Related