假设有枚举类型
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
参考链接:
[url]http://stackoverflow.com/questions/1104975/for-loop-to-iterate-over-enum-in-java[/url]
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
参考链接:
[url]http://stackoverflow.com/questions/1104975/for-loop-to-iterate-over-enum-in-java[/url]
本文介绍了一种在Java中遍历枚举类型所有值的方法,通过使用编译器隐式声明的.values()方法实现。该方法虽不在官方文档中列出,但实际存在并可供开发者使用。
5304

被折叠的 条评论
为什么被折叠?



