假设有枚举类型
public enum Direction {
NORTH,
NORTHEAST,
EAST,
SOUTHEAST,
SOUTH,
SOUTHWEST,
WEST,
NORTHWEST
}
如何遍历获取所有的值?
解决方案,利用java编译器隐式声明的.values()方法
for (Direction dir : Direction.values()) {
// do what you want
}
This values() method is implicitly declared by the compiler. So it is not listed on Enum doc.
It is an implicit method that exists only in the compiler. Therefore the base class can not declare a method with the same name and thus it does not get included in the automatically generated Javadocs
参考链接:
http://stackoverflow.com/questions/1104975/for-loop-to-iterate-over-enum-in-java
Java遍历枚举类型(iterate enum in java)
猜你喜欢
转载自darrenzhu.iteye.com/blog/2233561
今日推荐
周排行