One, iterator overview 1, what is an iterator?
In Java, there are a lot of data containers, for these operations have a lot of commonality. Java uses iterators to provide a common operating interface for various containers. This allows the traversal of the container to be isolated from its specific underlying implementation to achieve the decoupling effect.
Three methods are defined in the iterator interface:
2, iterators use
void Main (string[] args) { list<string> list=new arraylist<>(); List.add ("abc") ; List.add ("EDF"); List.add ("Ghi" for(iterator<string> it=list.iterator (); It.hasnext ();) {System.out.println (It.next ());} }
Execution Result:
Go back to the top second, ArrayList iterator realization
Implements Iterator<e> { int cursor; //// int expectedmodcount = Modcount, .... }
An internal class ITR is defined within ArrayList, which implements the iterator interface.
In ITR, there are three variables, namely
Cursor: Indicates the index position of the next element
Lastret: Represents the index position of the previous element
Expectmodcount: Number of times expected to be modified
Here's a look at the ITR class implements the three methods of the iterator interface:
Boolean Hasnext () { return cursor! = size; when the cursor is not equal to size, there is still an index element}
Public E Next ()// {checkforcomodification (); int i = cursor; if (i >= size) throw Span style= "COLOR: #0000ff" >new nosuchelementexception (); object[] Elementdata = ArrayList. this.elementdata; if (i >= Elementdata.length) throw new Concurrentmodificationexception (); cursor = i + 1; return (E) elementdata[lastret = i];}
There is a checkforcomodification () method in the next () method, which is now:
void checkforcomodification () { if (modcount! = expectedmodcount) new Concurrentmodificationexception (); }
As you can see, the function is used to determine whether the number of changes to the collection is legitimate.
Maintains a field within the collection Modcount is used to record the number of times the collection has been modified, modcount+1 whenever the internal structure of the collection changes (Add,remove,set).
A field Expectedmodcount is also maintained inside the iterator, which also records the number of times the current collection has been modified, initialized to the Modcount value of the collection. When we call iterator for a traversal operation, if there are other threads modifying the list, the Modcount!=expectedmodcount will appear. It will report concurrent modification of exception java.util.ConcurrentModificationException. The following is the sample code:
Publicstatic void main (string[] args) {arraylist<string> alist=new arraylist<string> (); Alist.add ("BBC" alist); iterator<string> it=alist.iterator (); while (It.hasnext ()) {if ( "ABC" .equals (It.next ())) {Alist.remove ("abc" alist);
In the above code, if we only use iterators to delete, there will be no concurrency modification exception errors.
public static void< Span style= "COLOR: #000000" > main (string[] args) {arraylist<string> alist=new Arraylist<string> (); Alist.add ("BBC" ); System.out.println ("Before removal:" +alist); iterator<string> it=alist.iterator (); while (It.hasnext ()) {if ( "ABC" .equals (It.next ())) {It.remove ();}} System.out.println ("After removal:" +alist);
Publicvoid remove () {if ( Lastret < 0) throw new IllegalStateException (); Checkforcomodification (); try {arraylist. This.remove (lastret), cursor = Lastret; Lastret = -1; Expectedmodcount = Modcount;} catch (Indexoutofboundsexception ex) {throw new Concurrentmodificationexception ();}}
When the remove operation is performed, Checkforcomodification () is executed, and then the Remove () method of ArrayList is executed, which adds 1 to the Modcount value, where we will expectedmodcount= Modcount, so that it remains unified.
Back to the top three, listiterator
As you can see above, iterator only provides a way to remove elements, what if we want to add elements to the traversal?
The Listiterator interface inherits the iterator interface, which allows the programmer to traverse the list in either direction, modify the list during iteration, and get the current position of the iterator in the list.
The Listiterator interface defines the following methods:
The following uses Listiterator to add element operations to the list by using the Edge traversal edge:
PublicStaticvoid main (string[] args) {arraylist<string> aLi st = new arraylist<string> (); Alist.add ("BBC" alist); listiterator<string> listit = Alist.listiterator (); while (Listit.hasnext ()) { if ("abc" .equals (Listit.next ())) {Listit.add ("haha") Span style= "color: #000000"); }} System.out.println ("Removed:" + alist);
Java Foundation--implementation of set iterator iterator