Explanation and analysis of Listiterator and iterator in Java

Source: Internet
Author: User

When using a Java collection, you need to use iterator. But there is also an iterator listiterator in the Java collection that can be used when using list, ArrayList, LinkedList, and vector. Here's a little bit of clarity when the iterator points to the position before the element, as shown in:

This assumes that the collection List consists of four elements List1, List2, List3, and List4, and when using the statement Iterator it = List.iterator (), the position of the iterator it points to is where the Iterator1 points to, When the statement it.next () is executed, the position that the iterator points to is moved to the position pointed to by Iterator2.

First look at the methods of iterator and Listiterator iterators.

Iterator iterators include the following methods:

Hasnext (): Returns trueif the iterator is pointing to a position that has elements after it,otherwise false

Next (): Returns the elements in the collection that follow the iterator point position

Remove (): Deletes the element in the collection that follows the iterator point

Listiterator iterators include the following methods:

Add (e E): Inserts the specified element into the list, before the insertion position is the current position of the iterator

Hasnext (): Returns True if there are elements behind the list iterator when the list is traversed in a forward direction , otherwise false

Hasprevious (): Returns trueif the list iterator is preceded by an element in the reverse loop,otherwise false is returned

Next (): Returns the element following the position of the listiterator in the list, and moves the iterator position back one bit,

Nextindex (): Returns the index of the element following the desired position of listiterator in the list, PS: If there is no element behind the iterator, and if there is an element behind it, the expected ordinal of the element

Previous (): Returns the element in the list that precedes the listiterator point

Previousindex (): Returns the index of the preceding element in the list where Listiterator is required , PS: Even if there is no element in the front of the iterator, it also returns the expected ordinal of the element, assuming that there is an element before it.

Remove (): Removes the last element returned by next () or previous () from the list (a bit of a mouthful, meaning that when you use the Hasnext () method on an iterator, you remove the element that Listiterator points to after the position When you use the Hasprevious () method on an iterator, delete listiterator points to the element preceding the position)

Set (E): Change the last element returned by next () or previous () from the list to the specified element e

A Same point

Are iterators, both of which can be used when the elements in the collection need to be traversed without interfering with their traversal process.

Two Different points

1. Using different scopes, iterator can be applied to all collections, set, list, and map, and the subtypes of those collections. Listiterator can only be used for lists and their subtypes.

2.ListIterator has the Add method, you can add objects to the list, and iterator cannot.

Both 3.ListIterator and iterator have Hasnext () and Next () methods, which can be traversed sequentially backwards, but Listiterator has hasprevious () and Previous () methods, Reverse (sequential forward) traversal can be implemented. Iterator not to be.

4.ListIterator can locate the current index position, Nextindex () and Previousindex () can be implemented. Iterator does not have this feature.

5. The delete operation can be implemented, but the Listiterator can implement the object modification, and the set () method can be implemented. Iterator can only traverse and cannot be modified.

Three: Iterator and listiterator usage examples

Listiterator usage:

1      Packagecom.collection; 2       3     Importjava.util.LinkedList; 4     Importjava.util.List; 5     ImportJava.util.ListIterator; 6       7     /** 8      * @authorZhu Wei9 * listiterator Test in linked listTen      *  One      */   A      Public classListiteratortest { -        -          Public Static voidMain (string[] args) { the             //TODO auto-generated Method Stub -List<string> staff =NewLinkedlist<>();  -Staff.add ("Zhuwei");  -Staff.add ("Xuezhangbin");  +Staff.add ("Taozhiwei");  -Listiterator<string> iter =Staff.listiterator ();  +String first =Iter.next ();  A                at             //Delete Zhuwei - Iter.remove ();  -                -             //change Zhuwei to Simei -             //iter.set ("Simei");  -System.out.println ("First:" +First );  in                -Iter.add ("Xiaobai");  to                +             //traversing the list element -System.out.println ("Iterate through the elements in list, method one:");  the              for(String str:staff) *System.out.println (str+ "");  $               Panax NotoginsengITER =Staff.listiterator ();  -System.out.println ("Iterate through the elements in list, method two:");  the              while(Iter.hasnext ()) +             {   A System.out.println (Iter.next ());  the             }   +         }   -        $}

Main contents turn from: http://blog.csdn.net/longshengguoji/article/details/41551491

Explanation and analysis of Listiterator and iterator in Java

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.