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. What is the difference between these two iterators? Let's analyze it in detail below. 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 after the Listiterator point in the list

Nextindex (): Returns the index of the element following the desired position in the list listiterator

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

Previousindex (): Returns the index of the preceding element in the list listiterator the desired position

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:

Package Com.collection;import java.util.linkedlist;import Java.util.list;import java.util.listiterator;/** * @author Zhu Wei * linked list listiterator Test * */public class Listiteratortest {public static void main (string[] args) {//TODO auto-generated m Ethod stublist<string> staff = new linkedlist<> () Staff.add ("Zhuwei"); Staff.add ("Xuezhangbin"); Staff.add ("Taozhiwei"); Listiterator<string> iter = Staff.listiterator (); String first = Iter.next ();//delete Zhuweiiter.remove ();//Change Zhuwei to Simei//iter.set ("Simei"); System.out.println ("First:" +first); Iter.add ("Xiaobai");//traverse the list element System.out.println ("traverse the list element, method one:"); for ( String str:staff) System.out.println (str+ "   "); iter = Staff.listiterator (); System.out.println ("Traverse list element, method two:"), while (Iter.hasnext ()) {System.out.println (Iter.next ());}}}


Listiterator and iterator in Java--a detailed analysis and discrimination

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.