Can I specify ordinal for enum in Java?

前端 未结 7 2053
时光说笑
时光说笑 2020-12-01 05:45

The ordinal() method returns the ordinal of an enum instance.
How can I set the ordinal for an enum?

7条回答
  •  旧时难觅i
    2020-12-01 06:51

    From http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html

    public final int ordinal()Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.

    Returns: the ordinal of this enumeration constant

    If you have

    public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

    then SUNDAY has an ordinal of 0, MONDAY is 1, and so on...

提交回复
热议问题