The scope and relationship of Iterator and ListIterator:
(1) Iterator can be used to implement the iterative interface List ArrayList, LinkedList, Map, etc.
(2) ListIterator, as the name implies, is used to iterate List to implement ArrayList and LinkedList.
(3) As can be seen from the source code or API documentation, Iterator is the parent class of ListIterator.
public interface ListIterator<E> extends Iterator<E> {// Omit...}New methods have been added to ListIterator.
Analysis of the iterator implicit cursor position instance:
As shown in the above figure, there are four elements a, b, c, d in the collection List to which the iterator is to iterate. When the xxx.iterator() or xxx.listIterator() method is called,
The iterator points to the position of Iterator_one, and when the xxx.next() method is called, the iterator position points to the Iterator_two position.
The position that the iterator points to is not directly opposite the element, but between the elements.
Methods in Iterator and ListIterator:
------ Method in Iterator:
Summary of official API methods:
Method analysis:
hasNext(): determines whether there is an element in the next position pointed by the element iterator in the collection. Return true, otherwise return false
next(): Returns the value of the next element at the position pointed by the iterator.
remove(): Remove the value removed by the iterator through the iterator.
------ Method in ListIterator:
Summary of official API methods:
Method analysis:
add(Ee): Insert a specified element in the collection. The insertion position is related to the last operation next() or previous() method.
If the last operation is next(), the element is inserted after the element removed next, and if it is previous(), the element is inserted before the element removed.
hasNext(): When the set is traversed in positive order, it determines whether the next element pointed to by the implicit cursor exists. If it exists, it returns true, otherwise it returns false.
hasPrevious(): When the set is reverse traversed, it determines whether the previous element of the implicit cursor exists. If it exists, it returns true, otherwise it returns false.
next(): Returns the next element value at the position pointed by the implicit cursor.
nextIndex(): Returns the index value of the next element at the position pointed by the implicit cursor.
previous(): Returns the previous element value at the position pointed by the implicit cursor.
previousIndex(): Returns the index value of the previous position element pointed to by the implicit cursor.
remove(): Deleting elements from the collection is not to delete all elements, but to delete the elements returned by the last operation next() or previous().
If next() or previous() does not return a value, an error will be reported.
set(Ee): Replace the element returned by next() or previous() with e from the list. For example, if next() is taken out as a, you can replace a with one by set.
Example 1:
package com.lanhuigu.java;import java.util.ArrayList;import java.util.List;import java.util.ListIterator;public class ListIteratorAPITest {public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("a");list.add("b");list.add("c");list.add("d");// At this time, the iterator points to the Iterator_one position ListIterator<String> listIterator = list.listIterator();// At this time, the iterator points to the Iterator_two position String firstElement = listIterator.next();// Delete the element listIterator.remove();// Delete the element retrieved by next. At this time, a has been deleted from the collection. Add element // ListIterator.previous(); listIterator.add("e");// Before pointing to the position Iterator_two, it is the previous position // Print the element retrieved by next. System.out.println("firstElement:" + firstElement);// Reinitialize the pointing position to the Iterator_one position, but a has not been deleted, and add e listIterator = list.listIterator(); while(listIterator.hasNext()) {System.out.println(listIterator.next());}System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- list.listIterator(); while(listIterator.hasNext()) {System.out.println(listIterator.next());}}}Example 2:
package com.lanhuigu.java;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.ListIterator;public class IteratorAPITest {public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("a");list.add("b");list.add("c");list.add("d");// The first way to traverse the set is to directly traverse through loop statements (for,do-while, while)// for for (String e : list) {System.out.println(e);}System.out.println("--------------------");// The second way to traverse the set (Iterator) Iterator<String> iterator = list.iterator(); while(iterator.hasNext()) {System.out.println(iterator.next());}System.out.println("-----------------------------");// The third way to traverse the set (ListIterator) ListIterator<String> listIterator = list.listIterator(); while(listIterator.hasNext()) {System.out.println(listIterator.next());}System.out.println("-------------------");}}Summarize
The above is all the detailed explanation of the Iterator and ListIterator examples in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!