Definition of Enumeration enumeration
Enumeration is also a new feature of JDK5.0.
JDK5.0 added a new type of "class"--enum type.
A new keyword enumis introduced for this purpose.
You can define an enumeration type like this:
public enum color{ Red, White, Blue,}
You can then use this:
Color MyColor = color.red;
Static methods for enumeration types
Enumeration types provide two useful static methods for values () and valueof ().
VALUES () returns an array that enumerates all the members.
Use of the values () method:
Use of the values () method for (color color:Color.values ()) { System.out.println (color); }
Will output:
Red
White
Blue
That is, the values () output defines all enumerated types in color.
Instead, valueof () converts the string to an enumeration value.
Use of enumerations
An enumeration can be used in a switch statement.
Example:
public class enumtest{public static void Dooperation (Opconstant opconstant) { switch (opconstant) { Case Turn_left: System.out.println ("Turn Left"); break; Case Turn_right: System.out.println ("Turn Right"); break; Case SHOOT: System.out.println ("shooting"); break; Default: Break ; } } public static void Main (string[] args) { dooperation (opconstant.shoot);} } Enum opconstant{ turn_left, Turn_right, SHOOT,}
The nature of enumerations
Let's look at an example:
public enum coin{ Penny ("Hello"), Nickle (' World '), Dime ("Welcome"), Quarter ("Hello World"); private String value; Public String GetValue () { return value; } Coin (String value) { this.value = value; } public static void Main (string[] args) { Coin Coin = coin.quarter; System.out.println (Coin.getvalue ());} }
Defining an enumeration type is essentially defining a category, but many details are done by the compiler, so the enum keyword acts like a class or interface in some way.
When you use "enum" to define an enumeration type, essentially the type you define inherits from the java.lang.Enum type, and the member of each enumeration is actually an instance of the enumeration type that you define (Instance ), they are all preset to final, so you cannot change them, they are also static members, so you can use them directly by type name, and most importantly, they are public ( public). That is, each member of the enumeration is public static final by default.
The nature of the enumeration:
The member of each enumeration is actually an instance of the enumeration type that you define (Instance).
When an enumeration type is defined, it can be compiled to determine How many instances of the enumeration type, and what the names of those objects are. You cannot use this enumeration type to create a new instance during run time.
Comparison of enumerations
Write one of the following programs:
public class showenum{public static void Main (string[] args) { Enumcompareto (opconstant.valueof (Args[0] )); } public static void Enumcompareto (Opconstant constant) { System.out.println (constant); The current input parameter is compared to the type in each enumeration and outputs the comparison result for (Opconstant c:opconstant.values ()) { System.out.println ( Constant.compareto (c));}}
Then pass in the parameter shoot inside the run configurations, the output is as follows:
SHOOT
2
1
0
If the incoming parameter turn_right:
Turn_right
1
0
-1
This program is used to compare enumeration members who are in front of who before, when input shoot, when shoot and each value is compared, it is in turn_left back two bits, in turn_right behind one, and itself compares output to 0.
Enumeration displays
Example:
public class showenum2{public static void Main (string[] args) {for (opconstant c:opconstant.values ()) { System.out.printf ("%d,%s,%n", c.ordinal (), c); } }}
Output:
0, Turn_left,
1, Turn_right,
2, SHOOT,
Enumset
The name of the Enumset describes its role, which is a new category added after J2SE 5.0, which assists you in creating a collection of enumeration values that provides a series of static methods that let you specify different ways to set up a collection.
The Enumset has multiple overloads of the of () method for constructing an enumeration collection that contains the specified enumeration object.
The corresponding Complementof () method returns the complement of an enumeration set.
The Noneof () method constructs an empty enumeration collection of the specified enumeration type. You can then add elements using the Add () method.
The CopyOf () method has two overloaded forms, one of which is the enumeration set Enumset, and the other is collection. Note You can use a collection to construct an enumeration collection, noting that if there are duplicate elements in collection, only one is added to the enumeration collection.
These methods are used in the following code example:
Import Java.util.list;import java.util.arraylist;import Java.util.enumset;import java.util.iterator;enum fontconstant{Plain, Bold, Italic, Hello,}public class enumsetdemo{public static void Main (string[] args) { System.out.println ("--------------of ()-----------------"); Of () method, constructs an enumeration collection containing the specified elements enumset<fontconstant> enumset = Enumset.of (Fontconstant.plain, Fontcon Stant. Bold); Showenumset (Enumset); System.out.println ("------------complementof ()---------------"); The Complementof () method constructs a complement of the specified enumeration collection Showenumset (Enumset.complementof (Enumset)); System.out.println ("--------------noneof ()-----------------"); The Noneof () method constructs an empty enumeration collection of the specified enumeration type enumset<fontconstant> EnumSet2 = enumset.noneof (Fontconstant.class); Enumset2.add (Fontconstant.italic); Showenumset (EnumSet2); System.out.println ("---------------copyOf ()------------------"); An overload of the CopyOf () method can be constructed by a collection of enumeration collections//First construct a list list<fontconstant> list = new arraylist<fontconstant> (); List.add (Fontconstant.bold); List.add (Fontconstant.italic); List.add (Fontconstant.plain); List.add (Fontconstant.bold); The Copyo method is then used to construct a enumset showenumset (enumset.copyof (list)); } public static void Showenumset (Enumset<fontconstant> enumset) {for (iterator<fontconstant> ite R = Enumset.iterator (); Iter.hasnext ();) {System.out.println (Iter.next ()); } }}
Enummap
Enummap's declaration is: Class enummap<k extends Enum<k>,v>, which indicates that the key is an enumeration type .
Examples of use are as follows:
Import Java.util.enummap;import Java.util.map;public class enummapdemo{public static void Main (string[] args) { map<action, string> Map = new enummap<action, string> (action.class); Map.put (Action.turn_left, "turn Left"); Map.put (Action.shoot, "shooting"); Map.put (Action.turn_right, "Turn Right"); For (Action Action:Action.values ()) { System.out.println (Map.get (action)); } }} Enum action{ turn_left, Turn_right, SHOOT,}
A summary of the Java-enumeration