Collection Framework-enumeration Interface enumeration

Source: Internet
Author: User


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&LT;STRING&GT;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.