java enum.values() 的顺序是否始终相同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11898900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
will the order for enum.values() always same
提问by M.J.
Possible Duplicate:
enum.values() - is an order of returned enums deterministic
I have an enum something like below :-
我有一个类似于下面的枚举:-
enum Direction {
EAST,
WEST,
NORTH,
SOUTH
}
If i say values()
on the Direction enum, then will the order of the value remain same all the time. I mean will the order of values will be in the below foramt always:
如果我values()
在方向枚举上说,那么值的顺序是否会一直保持不变。我的意思是值的顺序将始终采用以下格式:
EAST,WEST,NORTH,SOUTH
or can the order change at any point of time.
或者订单可以随时更改。
回答by Ajay George
Each Enum
type has a static values
method that returns an array containing all of the values of the enum type in the order they are declared.
每种Enum
类型都有一个静态values
方法,该方法返回一个数组,该数组包含按声明顺序排列的枚举类型的所有值。
This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.
此方法通常与 for-each 循环结合使用以迭代枚举类型的值。
Java 7 Spec documentation link : http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2
Java 7 规范文档链接:http: //docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2
回答by assylias
The behaviour of that method is defined in the Java Language Specification #8.9.2:
该方法的行为在Java Language Specification #8.9.2 中定义:
In addition, if E is the name of an enum type, then that type has the following implicitly declared static methods:
此外,如果 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();
回答by Heiko Schmitz
As the previous 2 answers say, the order is the order of declaration. However, it is not good practice to rely on this order (or on the enum's ordinal). If someone reorders the declaration or adds a new element in the middle of the declaration, the behavior of the code may change unexpectedly.
If there is a fixed order, I'd implement Comparable
.
正如前两个答案所说,顺序是声明的顺序。但是,依赖此顺序(或枚举的序数)并不是一个好习惯。如果有人对声明重新排序或在声明中间添加新元素,则代码的行为可能会意外更改。如果有固定的顺序,我会实施Comparable
.
回答by Kumar Vivek Mitra
Enums
are used to restrict the values of the variables to one of the only declared values in the enumerated list.
Enums
用于将变量的值限制为枚举列表中唯一声明的值之一。
These values are public static final
, ie (Constant objects of that specific Enum Type), and its Order is very Important for mapping the variables to these Objects.
这些值是public static final
,即(该特定枚举类型的常量对象),其顺序对于将变量映射到这些对象非常重要。
values()
is a static method of Enum
, which always returns the values in same order.
values()
是 a static method of Enum
,它总是以相同的顺序返回值。