Java literacy interface Enumeration
Java literacy interface Enumeration
Abstract: Original Source: http://www.cnblogs.com/Alandre/ sediment tile pulp carpenter hope to reprint, retain abstract, thank you!
I remember that two years ago, the greatest part of my high school life was the criticism of my teachers. Thank you! -Sand, brick, and tile pulp carpenter
I. First recognized Enumeration
When I wrote network programming, I found this interface. It is amazing that it has never been used before. NetworkInterface. getNetworkInterfaces () returns the NetworkInterface object of the Enumeration carrier. Enumeration is an interface class in java. util. It encapsulates methods for Enumeration data sets in Enumeration. The Enumeration interface is not a data structure. However, it is very important for other data structures. The Enumeration interface defines a means to obtain continuous data from a data structure.
Note: The functions of this interface are the same as those of the Iterator interface. In addition, the Iterator interface adds an optional remove operation and uses a shorter method name. The Iterator interface rather than the Enumeration interface should be preferred for the new implementation. Ii. Use java. util Enumeration
Generally, the following two methods in Enumeration are used to print all elements in the vector:
(1)boolean
hasMoreElements()
Test whether the enumeration contains more elements.
(2)E
nextElement()
If this enumeration object has at least one available element, the next element of this enumeration is returned.
Usage 1:
Vec is an object that implements a Vector (SET) class through an interface. vec. elements () gets an element of a Vector (SET.
for(Enumeration e = vec.elements(); e.hasMoreElements();){
System.out.println(e.nextElement());
}
Usage 2:
E is an object that implements the Enumeration interface
while (e.hasMoreElements()) { Object o= e.nextElement(); System.out.println(o);}
The following is an example:
public class TestEnumeration{ public static void main(String[] args){ Vector v = new Vector(); 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
Iii. demonstrate the use of the Enumeration Interface
Below we will implement this interface to complete a small function:
The MyDataStruct class is used to instantiate a simple enumeration object that can be provided to the data result object of the program.
1234567891011121314151617181920 |
class MyDataStruct { String[] data; // Constructor MyDataStruct() { data = new String[ 4 ]; data[ 0 ] = "zero" ; data[ 1 ] = "one" ; data[ 2 ] = "two" ; data[ 3 ] = "three" ; } // Return an enumeration object to the application Enumeration getEnum() { return new MyEnumeration( 0 , data.length, data); } } |
The MyEnumeration class implements the Enumeration interface.
Class MyEnumeration implements Enumeration {int count; // counter int length; // The length of the stored array Object [] dataArray; // reference for storing data arrays // constructor MyEnumeration (int count, int length, Object [] dataArray) {this. count = count; this. length = length; this. dataArray = dataArray;} public boolean hasMoreElements () {return (count <length);} public Object nextElement () {return dataArray [count ++];}
Test MainClass
Class MainClass {public static void main (String [] args) {// instantiate MyDataStruct-type object MyDataStruct mySataStruct = new MyDataStruct (); // obtain the enumeration object Enumeration myEnumeration = mySataStruct that describes the object of myDataStruct type. getEnum (); // use the object loop to display each element of an object of the myDataStruct type while (myEnumeration. hasMoreElements () System. out. println (myEnumeration. nextElement ());}}
You can see the following output:
zeroonetwothree
4. Thank you for your knowledge sources and summary.
I. First recognized Enumeration
Ii. Use of functions
Iii. demonstrate the use of the Enumeration Interface
From: java Basics
Calligraphy is the product of human art and beauty