Java-enumeration Summary

Source: Internet
Author: User

The paper came to the end of shallow, I know this matter to preach-Luask the canal that is clear so, for have fountainhead come--Zhu Xi

The enumeration (enum) interface acts like iterator, providing only the ability to traverse the elements of a collection of vector and Hashtable types, without supporting the removal of elements.
Java8 the source of the enumeration interface:
Public Interfaceenumeration<E> {
/**
* Tests If this enumeration contains more elements.
*
* @return<code>true</code>If and only if the This enumeration object
* Contains at least one + element to provide;
* <code>false</code> otherwise.
*/
Booleanhasmoreelements();//Determine if the element is included

/**
* Returns the next element of this enumeration if this enumeration
* object has at least one more element to provide.
*
* @returnThe next element of this enumeration.
* @exception nosuchelementexception If no more elements exist.
*/
Enextelement();//Get the next element
}
Through the enumeration source analysis can be obtained, enumeration has two methods: (1) Boolean hasmoreelements ();//whether there are elements, if there is a return true, otherwise the expression contains at least one element (2) E Nextelement ();//If the enumeration enumeration object also has elements that return only the next element of the object, the Nosuchelementexception exception is thrown.
Simple example:
Public classtestenumeration{
Public static voidMain(string[] args) {
Vector v =NewVector ();
V.addelement ("Lisa");
V.addelement ("Billy.");
V.addelement ("Mr Brown");
Enumeration E = V.elements ();//Return Enumeration object
while(E.hasmoreelements ()) {
String value = (string) e.nextelement ();//Call the Nextelement method to get the element
System. out. Print (value);
}
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java-enumeration Summary

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.