An iterator (Iterator), which belongs to an object that is responsible for selecting within the collection
Elements and provide them to the user of the inheritors. As a class, it also provides a level of abstraction. Philip
With this level of abstraction, you can isolate the collection details from the code used to access that collection. By inheriting the
function, the set is abstracted into a simple sequence. The inheritors allows us to traverse that sequence without needing
Care what the infrastructure is-in other words, whether it's a vector, a list of links, a stack,
or something else. In this way, we have the flexibility to change the underlying data without
code to cause interference. Java at the very beginning (in versions 1.0 and 1.1) provides a standard successor,
Named enumeration (enumeration), which provides services for all of its collection classes. Java 1.2 Adds a more complex
Miscellaneous collection library, which contains a successor named iterator, which can do more than the old-fashioned enumeration
More things.
1. The main differences between iterator and enumeration
Iterator is also an interface, its source code is as follows:
Package Java.util; Public Abstract Interface Iterator<e>{ publicabstractboolean hasnext (); Public Abstract E Next (); Public Abstract void remove ();
View Code
Enumeration is an interface, its source code is as follows:
Package Java.util; Public Abstract Interface Enumeration<e>{ publicabstractboolean hasmoreelements (); Public Abstract E nextelement ();}
View Code
(1) The collection class in Java provides a way to return iterator, that is, an iterator, the main difference between it and enumeration (enumeration) is that iterator can delete elements, but enumration cannot.
(2) It is also important to note that when you use iterator to traverse a collection, you should use the iterator remove () method to remove elements from the collection, using the collection's remove () The Concurrentmodificationexception method throws an exception.
(3) The function of the enumeration interface and the function of the Iterator interface are duplicated. 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.
(4) iterators are different from enumerations with two points:
• Iterators allow callers to take advantage of well-defined semantics to remove elements from the collection that the iterator points to during the iteration.
• The method name has been improved.
2, iterator in Hasnext (), Next (), remove () Three methods in which class are implemented
By looking at the source code in the Rt.jar and viewing the JDK-API documentation, it is found that many classes implement this interface using an internal class and then return the class to you using the method
This is the ergodic mode such as LinkedList, ArrayList and so on.
ArrayList internal implementations are as follows:
PublicIterator<e>iterator () {return NewITR (NULL);}Private classItrImplementsIterator<e>{intcursor;intLastret =-1;intExpectedmodcount = ArrayList. This. Modcount;PrivateItr () {} Public BooleanHasnext () {return This. cursor! = ArrayList. This. Size;} PublicE Next () {checkforcomodification ();inti = This. Cursor;if(I >= ArrayList. This. Size)Throw Newnosuchelementexception (); object[] Arrayofobject= ArrayList. This. Elementdata;if(I >=arrayofobject.length)Throw Newconcurrentmodificationexception (); This. Cursor = (i + 1);returnarrayofobject[( This. Lastret =i)];} Public voidRemove () {if( This. Lastret < 0)Throw Newillegalstateexception (); Checkforcomodification ();Try{ArrayList. This. Remove ( This. Lastret); This. cursor = This. Lastret; This. Lastret = 1; This. Expectedmodcount = ArrayList. This. Modcount;}Catch(indexoutofboundsexception localindexoutofboundsexception) {Throw Newconcurrentmodificationexception ();}}Final voidcheckforcomodification () {if(ArrayList. This. modcount! = This. Expectedmodcount)Throw Newconcurrentmodificationexception ();}}
View Code
Iterator and enumeration