我有一个类似下面的枚举: -
enum Direction {
EAST,
WEST,
NORTH,
SOUTH
}
Run Code Online (Sandbox Code Playgroud)
如果我values()在Direction enum上说,那么值的顺序将始终保持相同.我的意思是值的顺序总是在下面的foramt中:
EAST,WEST,NORTH,SOUTH
Run Code Online (Sandbox Code Playgroud)
或者订单可以在任何时间点改变.
Aja*_*rge 26
每种Enum类型都有一个静态values方法,该方法按照声明的顺序返回一个包含枚举类型的所有值的数组.
此方法通常与for-each循环结合使用,以迭代枚举类型的值.
Java 7 Spec文档链接:http: //docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2
该方法的行为在Java语言规范#8.9.2中进行了定义:
此外,如果E是枚举类型的名称,则该类型具有以下隐式声明的静态方法:
/**
* Returns an array containing the constants of this enum
* type, in the order they're declared. This method may be
* used to iterate over the constants as follows:
*
* for(E c : E.values())
* System.out.println(c);
*
* @return an array containing the constants of this enum
* type, in the order they're declared
*/
public static E[] values();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12867 次 |
| 最近记录: |