Enumeration and Iterator interface

Source: Internet
Author: User

Enumeration interface

The traversal of elements in HashTable and vectors is implemented using the enumeration interface.

hasMoreElements ();// determine If there are elements in enumeration

Nextelement ();// returns to the next element

  Iterator Interface

An iterator over a collection. Iterator takes the place of enumeration in the Java collections Framework. Iterators differ from enumerations in and ways:

  • Iterators allow the caller-remove elements from the underlying collection during the iteration with well-defined semant Ics.
  • Method names has been improved.
  • the former is"Fail-fast"of the--fail-fast collection (Collection) fail-fast events. For example: When a thread a through iterator go through the process of a set, if the contents of the collection are changed by other threads; then thread a When the collection is accessed, abnormal, resulting fail-fast event ;
  • The former can cooperate with The iterable<e> interface is used in the For-each loop of Java 5 .
  • Iterator is suitable for scenes with unknown boundaries, and the bounds of enumeration are known and enumerable. And it's two things that work differently.

Method:

Hasnext (); next (); remove (); foreachremaining (ConsumerSuper e> action);

The last method is equivalent to executing each element of the collection:

while (Hasnext ())

Action.accept (Next ());

Listiterator Interfaceinherit fromIteratorinterface, but several new methods have been added

An iterator for lists This allows the programmer to traverse the list in either direction, modify the list during Iteratio N, and obtain the iterator ' s current position in the list. A listiterator have no current element; Its cursor position always lies between the element, would be returned by a call to previous () and the Elemen T that would is returned by a call to next (). An iterator for a list of length n have n+1 possible cursor positions, as illustrated by the carets (^) below:

Element (0) Element (1) Element (2) ... Element (n-1)

Cursor Positions: ^ ^ ^ ^ ^

voidHasprevious ();//determines whether there are elements in front of the cursor;Object Previous ();//returns the element that precedes the cursor, while the cursor moves forward one bit. There is no element in the cursor before the java.util.NoSuchElementException error, so it is best to judge before use;intNextindex ();//returns the index position of the element behind the cursor, initially 0, and N at the end of the traversal of n elements ;intPreviousindex ();//returns the position of the element in front of the cursor, starting at 1 and reporting java.util.NoSuchElementException error;voidAdd (E);//inserts an element in front of the cursorvoidSet (E);//Update iterator The element of the last operation is E, which is the update of the element returned by the last call to next () or previous (). The java.lang.IllegalStateException is thrown when there is no iteration, that is, no call to next () or previous () to call set directly;

Note:the Remove () and set (Object) methods do not operate according to the position of the cursor, they are defined as operations by next () or previous () The last element returned by the call.

Enumeration and Iterator interface

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.