Java LinkedList's Listiterator

Source: Internet
Author: User

Java LinkedList's Listiterator

@author Ixenos

Listiterator<e> is an interface that inherits from Iterator<e>

Therefore, Listiterator Note:

1. iterators do not store references to all elements, only two pointers, one pointer to the last returned element, and another element that is not in the next step;

2. Synchronize the internal and external modcount before the iteration begins, check for synchronization during the iteration, and if the external structure changes, the iteration fails quickly (fast-fails mechanism);

  3.ListIterator can be counted as half a linkedlist view because the external structure (add, remove) can be modified on the way to the iteration.

Listiterator (int index) source analysis

Listiterator
Listiterator (int index)
Returns a list iterator (in the appropriate order) for the elements in this list, starting at the specified position in the list .
Parameters:
index-The index of the first element to be returned from the list iterator (by calling the next method)
Return:
The Listiterator (in the appropriate order) of the elements in this list, starting at the specified position in the list
Thrown:
IndexOutOfBoundsException-If the index is out of range (index < 0 | | index > Size ())
Pass:
   Internal classes Private class Listitr implements listiterator<e> implementation
1 //iterators do not store numeric values! is to return one time per iteration2 //a powerful Listiterator method in LinkedList3 4    PublicListiterator<e> Listiterator (intindex) {5 Checkpositionindex (index);6         return NewLISTITR (index);//calling an anonymous object of the inner class Listitr7     }8 9 //the Listiterator interface is given to the internal class implementation for compatibility with the iterator interface because the Listiterator interface inherits from the iterator interfaceTen     Private classListitrImplementslistiterator<e> {//implements the Listiterator interface One         PrivateNode<e> lastreturned;//points to the last returned element A         PrivateNode<e> Next;//point to the next non-involved element -         Private intNextindex; -         Private intExpectedmodcount =Modcount; the  -Listitr (intindex) { -             //assert Ispositionindex (index); -Next = (index = = size)?NULL: node (index); +Nextindex =index; -         } +  A          Public BooleanHasnext () { at             returnNextindex <size; -         } -  -          PublicE Next () { - checkforcomodification (); -             if(!Hasnext ()) in                 Throw Newnosuchelementexception (); -  tolastreturned =Next; +Next =Next.next; -nextindex++; the             returnLastreturned.item; *         } $ Panax Notoginseng          Public Booleanhasprevious () { -             returnNextindex > 0; the         } +  A          PublicE Previous () { the checkforcomodification (); +             if(!hasprevious ()) -                 Throw Newnosuchelementexception (); $  $lastreturned = Next = (Next = =NULL) ?Last:next.prev; -nextindex--; -             returnLastreturned.item; the         } - Wuyi          Public intNextindex () { the             returnNextindex; -         } Wu  -          Public intPreviousindex () { About             returnNextIndex-1;  $         } -      - //can delete yo -          Public voidRemove () { ACheckforcomodification ();//Make sure the external modcount is not changed . +             if(lastreturned = =NULL) the                 Throw Newillegalstateexception (); -  $Node<e> Lastnext =Lastreturned.next; the unlink (lastreturned); the             if(Next = =lastreturned) theNext =Lastnext; the             Else -nextindex--; inlastreturned =NULL; theexpectedmodcount++;//Remove external elements modcount++ so the internal expectedmodcount is also + + to synchronize the         } About  the          Public voidset (e e) { the             if(lastreturned = =NULL)//Make sure the external modcount is not changed . the                 Throw Newillegalstateexception (); + checkforcomodification (); -Lastreturned.item =e; the         }Bayi  the          Public voidAdd (e e) { the checkforcomodification (); -lastreturned =NULL; -             if(Next = =NULL) theLinklast (e);//If the next pointer is at the end of the team, add it directly to the tail . the             Else theLinkbefore (E, next);//Otherwise, insert the next pointer to the front of the element thenextindex++; -expectedmodcount++;//Remove external elements modcount++ so the internal expectedmodcount is also + + to synchronize the  the         } the 94          Public voidForeachremaining (consumer<?SuperE>action) { theObjects.requirenonnull (action);//throws a null pointer exception if action is empty the              while(Modcount = = Expectedmodcount && Nextindex <size) { the action.accept (next.item);98lastreturned =Next; AboutNext =Next.next; -nextindex++;101             }102 checkforcomodification ();103         } 104  the //An external structure modification is an iterative fast failure fast-fails106         Final voidcheckforcomodification () {107             if(Modcount! =expectedmodcount)108                 Throw Newconcurrentmodificationexception ();109         } the}
Listiterator Source

Descending iterations with LISTITR implementation

Descendingiterator
Descendingiterator ()
returns an iterator that iterates over the elements of this double-ended queue in reverse order. Elements are returned in the order from the last (tail) to the first (head).
The essence is to modify the pointer in the key operation
Return:
An iterator that iterates over the elements of this double-ended queue in reverse order
Start from the following versions:
1.6
1   /**Descending Iteration2      * @since1.63      */4 //sort is the order ascending means that the ascending descending represents the descending5      PublicIterator<e>Descendingiterator () {6         return Newdescendingiterator ();7     }8 9     /**Ten * Adapter to provide descending iterators via listitr.previous One      */ A //using Lisitr to implement descending iterations -     Private classDescendingiteratorImplementsIterator<e> { -         Private FinalListitr ITR =Newlistitr (Size ());  the          Public BooleanHasnext () { -             returnItr.hasprevious ();//using the original method, we changed the return -         } -          PublicE Next () { +             returnItr.previous ();//using the original method, we changed the return -         } +          Public voidRemove () { A Itr.remove (); at         } -}
Descendingiterator Source

Java LinkedList's Listiterator

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.