Abstract: Abstract: Original creation Place: http://www.cnblogs.com/Alandre/sediment brick slurry Carpenter hope reprint, keep abstract, thank you!
Remember 2 years ago, the most high school life is the teacher's criticism. Thank you! -Mud and brick pulp carpenter
I. Initial knowledge of enumeration
Two. Use of functions
Three. Demonstrating the use of the enumeration interface
I. Initial knowledge of enumeration
When I was writing network programming, I found this interface. It's amazing that none of them used to be, networkinterface.getnetworkinterfaces () returns the NetworkInterface object of the enumeration carrier. Enumeration is an interface class in Java.util that encapsulates methods for enumerating data collections in enumeration. The enumeration interface itself is not a data structure. However, it is important for other data structures. The enumeration interface defines the means by which continuous data is obtained from a data structure.
Note: The functionality of this interface is duplicated with the functionality of the Iterator interface. Additionally, the Iterator interface adds an optional remove operation and uses a shorter method name. The new implementation should prioritize the use of the Iterator interface instead of the enumeration interface. Two. Use of the function Java.util enumeration<e>
All elements in a vector are usually printed with the following two methods in enumeration:
(1)boolean
hasMoreElements()
Tests whether this enumeration contains more elements.
(2)E
nextElement()
If this enumeration object has at least one element that can be supplied, the next element of this enumeration is returned.
Usage One:
VEC is an object of the interface implementation class vector (collection), and Vec.elements () obtains an element of the vector (collection).
for (Enumeration e = vec.elements (); e.hasmoreelements ();) {
}
Usage Two:
E is a object that implements the enumeration interface
while (E.hasmoreelements ()) { Object o= e.nextelement (); System.out.println (o);}
Here's an example:
Public classtestenumeration{ Public Static voidMain (string[] args) {Vector v=NewVector (); V.addelement ("Li"); V.addelement ("YYY"); V.addelement ("Brown"); Enumeration e=v.elements (); while(E.hasmoreelements ()) {String value=(String) e.nextelement (); System.out.println (value); } }}
You can see the following output
Liyyybrown
Three. Demonstrating the use of the enumeration interface
Here we implement this interface to complete a small function:
The Mydatastruct class is used to instantiate a simple, enumeration object that can be supplied to a data result object that uses the program
Class mydatastruct{ string[] data; Constructor mydatastruct () { data = new String[4]; Data[0] = "zero"; DATA[1] = "one"; DATA[2] = "both"; DATA[3] = "three"; } Returns a enumeration object to the user program enumeration getenum () { return new myenumeration (0, Data.length, data);} }
Myenumeration class implements enumeration interface
classMyenumerationImplementsenumeration{intCount//counter intLength//the length of the stored arrayObject[] DataArray;//storing a reference to an array of data//ConstructorsMyenumeration (intCountintlength, object[] dataarray) { This. Count =count; This. length =length; This. DataArray =DataArray; } Public Booleanhasmoreelements () {return(Count <length); } PublicObject nextelement () {returndataarray[count++]; }}
Test class MainClass
classmainclass{ Public Static voidMain (string[] args) {//instantiating an object of type MydatastructMydatastruct mysatastruct =Newmydatastruct (); //Gets the enumeration object that describes the Mydatastruct type ObjectEnumeration myenumeration =Mysatastruct.getenum (); //use an object loop to display each element in an object of type Mydatastruct while(Myenumeration.hasmoreelements ()) System.out.println (Myenumeration.nextelement ()); }}
You can see the following output:
Zeroonetwothree
Four. Thanks to the source of knowledge and summary
I. Initial knowledge of enumeration
Two. Use of functions
Three. Demonstrating the use of the enumeration interface
From: Java Basics
Calligraphy is the product of man's art and beauty