我有一个 Java 的enum
主要和中间方向:
public enum Direction {
NORTH,
NORTHEAST,
EAST,
SOUTHEAST,
SOUTH,
SOUTHWEST,
WEST,
NORTHWEST
}
如何编写迭代这些enum
值的for
循环?
枚举值#():
for (Direction d : Direction.values()) {
System.out.println(d);
}
你可以这样做:
for (Direction direction : EnumSet.allOf(Direction.class)) {
// do stuff
}