Home > Java > javaTutorial > Detailed explanation and analysis of ListIterator and Iterator in Java

Detailed explanation and analysis of ListIterator and Iterator in Java

高洛峰
Release: 2016-12-13 17:20:45
Original
1275 people have browsed it

When using java collections, you need to use Iterator. But there is also an iterator ListIterator in the java collection, which 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. One thing that needs to be made clear here is that the position pointed by the iterator is the position before the element, as shown in the following figure:

Detailed explanation and analysis of ListIterator and Iterator in Java

Here it is assumed that the collection List consists of four elements List1, List2, List3 and List4. When using the statement Iterator When it = List.Iterator(), the position pointed by the iterator it is the position pointed by Iterator1 in the above figure. After the statement it.next() is executed, the position pointed by the iterator moves to the position pointed by Iterator2 in the above figure.

First, let’s take a look at the methods of Iterator and ListIterator.

Iterator contains the following methods:

hasNext(): If there is an element after the position pointed by the iterator, return true, otherwise return false

next(): Return the element after the position pointed by the Iterator in the set

remove(): Delete the element in the collection after the position pointed by the Iterator. The methods included in the ListIterator are:

add(E e): Insert the specified element into the list, and the insertion position is before the current position of the iterator.

hasNext( ): When traversing the list in the forward direction, if there are elements after the list iterator, it returns true, otherwise it returns false

hasPrevious(): If it traverses the list in the reverse direction, and there are elements in front of the list iterator, it returns true, otherwise Returns false

next(): Returns the element in the list after the position pointed by ListIterator

nextIndex(): Returns the index of the element after the position required by ListIterator in the list

previous(): Returns the element in the list before the position pointed by ListIterator

previousIndex(): Returns the index of the element in front of the desired position of ListIterator in the list

remove(): Removes the last element returned by next() or previous() from the list (a bit confusing, it means using hasNext() on the iterator ) method, delete the element after the position pointed by ListIterator; when using the hasPrevious() method on the iterator, delete the element before the position pointed by ListIterator)

set(E e): Remove next() or previous() from the list The last element returned The last element returned is changed to the specified element e

one. The same thing

are iterators. When you need to traverse the elements in the collection without interfering with the traversal process, both iterators can be used.

2. Differences

1. Different usage scope, Iterator can be applied to all collections, Set, List and Map and subtypes of these collections. ListIterator can only be used for List and its subtypes.

2. ListIterator has an add method, which can add objects to the List, but Iterator cannot.

3. Both ListIterator and Iterator have hasNext() and next() methods, which can realize sequential backward traversal, but ListIterator has hasPrevious() and previous() methods, which can realize reverse (sequential forward) traversal. Iterator cannot.

4.ListIterator can locate the position of the current index, and nextIndex() and previousIndex() can achieve this. Iterator does not have this functionality.

5. Both can implement deletion operations, but ListIterator can implement object modification, and the set() method can achieve it. Iterator can only be traversed 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;

public class ListIteratorTest {
 public static void main(String[] args) {  // TODO Auto-generated method stub  List<String> staff = new LinkedList<>();  staff.add("zhuwei");  staff.add("xuezhangbin");  staff.add("taozhiwei");  ListIterator<String> iter = staff.listIterator();  String first = iter.next();    //删除zhuwei  iter.remove();    //把zhuwei改为simei  //iter.set("simei");  System.out.println("first:"+first);    iter.add("xiaobai");    //遍历List元素  System.out.println("遍历List中元素,方法一:");  for(String str : staff)   System.out.println(str+"  ");    iter = staff.listIterator();  System.out.println("遍历List中元素,方法二:");  while(iter.hasNext())  {   System.out.println(iter.next());  } }
}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template