- Enumeration Interface Definition
- Enumeration interface and iterator interface usage comparison
One
1. Enumeration Interface Definition
public interface Enumeration<e> implements the enumeration interface object, which generates a series of elements, one at a time. Successive calls to the Nextelement method will return a series of contiguous elements.
For example, to output all elements of vector<e> v , you can use the following methods:
For (enumeration<e> E = v.elements (); e.hasmoreelements ();)
System.out.println (E.nextelement ());
These methods are enumerated primarily through the elements of the vector, the keys of the Hashtable, and the values in the hash table. The enumeration is also used to assign an input stream to Sequenceinputstream.
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 .
2, two methods
hasmoreelements () to test whether this enumeration contains more elements.
nextelement () returns the next element of this enumeration if there is at least one element that can be supplied by this enumeration object.
Two
1, enumeration interface and iterator interface usage comparison, the use of both interface methods can be used to traverse the collection elements, the former is now only used in special occasions, the latter is now widely used.
2, the code is as follows:
PackageAction;Importjava.util.Enumeration;ImportJava.util.Iterator;ImportJava.util.Vector; Public classTest { Public Static voidMain (string[] args) {Vector dayNames=NewVector (); Daynames.add ("Sunday"); Daynames.add ("Monday"); Daynames.add ("Tuesday"); Daynames.add ("Wednesday"); Daynames.add ("Thursday"); Daynames.add ("Friday"); Daynames.add ("Saturday"); //traversing with iterator iteratorsIterator it =Daynames.iterator (); while(It.hasnext ()) {String name=(String) it.next (); SYSTEM.OUT.PRINTLN (name); } System.out.println ("##################### Demarcation Line ####################"); //traversing using the enumeration interfaceEnumeration days =daynames.elements (); while(Days.hasmoreelements ()) {System.out.println (days.nextelement ()); } }}
3. The results are as follows:
sundaymondaytuesdaywednesdaythursdayfridaysaturday##################### dividing line #################### Sundaymondaytuesdaywednesdaythursdayfridaysaturday
Java Enumeration Interface