我有兩個數組:牆和鄰居。在Java中使用Enum的序數值來索引數組是不好的做法嗎?
public boolean[] walls = new boolean[4];
public Cell[] neighbors = new Cell[4];
,我有一個枚舉:
enum Dir
{
North,
South,
East,
West
}
現在,我希望能夠通過自己的方向來訪問牆壁或鄰居,所以我不必繞過一堆魔索引。
但是,當我閱讀Enum.ordinal()的文檔時,它說程序員幾乎不會使用這種方法,這使得我認爲它不應該以這種方式使用。
我的想法做類似的:
List<Dir> availableDirections = new ArrayList<Dir>();
for(Dir direction : Dir.values())
if (!Neighbors[direction.ordinal()].Visited)
availableDirections.add(direction);
甚至:
return Neighbors[Dir.North.ordinal()];
我應該恢復使用靜態常量的北,南,東,西與設置爲索引值他們還是使用枚舉的序數方法?
'EnumSet'用於'walls'。可能不需要它被同步。 – 2009-10-15 19:47:55