For:
Enum Color {Red,blue,black Yellow,green};
(1) ordinal () method: Returns the order of enumerated values in the enumeration class. This order depends on the order in which the enumeration values are declared.
Color.RED.ordinal (); Return Result: 0
Color.BLUE.ordinal (); return Result: 1
(2) CompareTo () method: An enum implements the Java.lang.Comparable interface, so it can be compared to the order of the specified object. The CompareTo in the enum returns the difference in the order of the two enumerated values. Of course, the premise is that two enumerated values must belong to the same enumeration class, otherwise the ClassCastException () exception is thrown. (Concrete visible source code)
Color.RED.compareTo (Color.Blue); return result-1
(3) VALUES () method: A static method that returns an array containing all the enumerated values.
Color[] Colors=color.values ();
for (Color c:colors) {
System.out.print (c+ ",");
}//return Result: Red,blue,black Yellow,green,
(4) ToString () method: Returns the name of an enumerated constant.
Color c=color.red;
System.out.println (c);//return Result: RED
(5) valueof () Method: This method corresponds to the ToString method and returns an enumerated constant with the specified enumeration type with the specified name.
Color.valueof ("BLUE"); return Result: Color.Blue
(6) Equals () method: Compares the references of two enumerated class objects.
Summarize:
1. Enum<->int
Enum-> Int:int i = enumType.value.ordinal ();
int-> enum:enumtype b= enumtype.values () [i];
2. enum<->string
Enum-> String:enumType.name ()
String-> enum:enumType.valueOf (name);