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

前端 未结 7 1255
不思量自难忘°
不思量自难忘° 2020-12-05 10:49

I have two arrays: Walls and Neighbors.

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

and I have an Enum:

7条回答
  •  忘掉有多难
    2020-12-05 11:36

    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.

提交回复
热议问题