public interface enumeration<e>
enumeration is an interface class in Java.util that encapsulates methods for enumerating data collections in enumeration. the Method Hawmoreelement () is provided in enumeration to determine whether the bundle in the collection has other elements and methods nextelement () to get the next element. Using these two methods, you can sequentially get the elements in the collection.
For example, to output vector<e> all elements of 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 specify the input stream SequenceInputStream
in.
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.
package cn.itcast.p5.vector; import java.util.arraylist;import java.util.collections; import java.util.enumeration;import java.util.iterator; public class vectordemo { public static void main (String[] args) { //vector<string> v = new Vector<String> (&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ARRAYLIST<STRING>&NBSP;V); = new ArrayList<String> (); v.add ("ABC1"); v.add ("ABC2"); V.add ("ABC3"); Iterator<string> it = v.iterator (); // while (It.hasnext ()) { &Nbsp; //system.out.println (It.next ()); //} /* Iterator<String> it = V.iterator (); enumeration<string> en = new enumeration <String> () { @Override public boolean hasmoreelements () { return it.hasnext (); } @Override public string nextelement () { return it.next (); } }; */ //gets the enumeration interface object in the collection. Enumeration<String> en = Collections.enumeration (v); while (En.hasMoreElements ()) { system.out.println (En.nextElement ()); } } }
Collection Framework-enumeration Interface enumeration