📌  相关文章
📜  JavaIterator 和 ListIterator 的区别

📅  最后修改于: 2021-09-11 03:44:25             🧑  作者: Mango

迭代器

Java中的 Collection 框架中使用迭代器来一一检索元素。它可以应用于任何 Collection 对象。通过使用 Iterator,我们可以执行读取和删除操作。每当我们想要枚举所有 Collection 框架实现的接口(如 Set、List、Queue、Deque)以及 Map 接口的所有实现类中的元素时,都必须使用迭代器。 Iterator 是唯一可用于整个集合框架的游标。

Iterator 对象可以通过调用 Collection 接口中的 iterator() 方法来创建。

// Here "c" is any Collection object. itr is of
// type Iterator interface and refers to "c"
Iterator itr = c.iterator();

列表迭代器
它仅适用于 List 集合实现的类,如 arraylist、linkedlist 等。它提供双向迭代。当我们要枚举 List 的元素时,必须使用 ListIterator。这个游标比迭代器有更多的功能(方法)。

ListIterator 对象可以通过调用 List 接口中的 listIterator() 方法来创建。

// Here "l" is any List object, ltr is of type
// ListIterator interface and refers to "l"
ListIterator ltr = l.listIterator();

Iterator 和 ListIterator 的区别:

  1. Iterator 只能向前遍历,而 ListIterator 可以向前和向后遍历。

    例子:

    import java.io.*;
    import java.util.*;
      
    class IteratorDemo1 {
        public static void main(String[] args)
        {
            ArrayList list
                = new ArrayList();
      
            list.add(1);
            list.add(2);
            list.add(3);
            list.add(4);
            list.add(5);
      
            // Iterator
            Iterator itr = list.iterator();
      
            System.out.println("Iterator:");
            System.out.println("Forward traversal: ");
      
            while (itr.hasNext())
                System.out.print(itr.next() + " ");
      
            System.out.println();
      
            // ListIterator
            ListIterator i = list.listIterator();
      
            System.out.println("ListIterator:");
            System.out.println("Forward Traversal : ");
      
            while (i.hasNext())
                System.out.print(i.next() + " ");
      
            System.out.println();
      
            System.out.println("Backward Traversal : ");
      
            while (i.hasPrevious())
                System.out.print(i.previous() + " ");
      
            System.out.println();
        }
    }
    
    输出:
    Iterator:
    Forward traversal: 
    1 2 3 4 5 
    
    ListIterator:
    Forward Traversal : 
    1 2 3 4 5 
    Backward Traversal : 
    5 4 3 2 1
    
  2. ListIterator 可以帮助替换元素,而 Iterator 不能。

    例子:

    import java.util.ArrayList;
    import java.util.ListIterator;
      
    public class ListIteratorDemo2 {
        public static void main(String[] args)
        {
      
            ArrayList aList
                = new ArrayList();
            aList.add(1);
            aList.add(2);
            aList.add(3);
            aList.add(4);
            aList.add(5);
      
            System.out.println("Elements of ArrayList: ");
            for (Integer i : aList) {
                System.out.println(i);
            }
            ListIterator l
                = aList.listIterator();
            l.next();
            l.set(80000);
      
            System.out.println("\nNow the ArrayList"
                               + " elements are: ");
            for (Integer i : aList) {
                System.out.println(i);
            }
        }
    }
    
    输出:
    Elements of ArrayList: 
    1
    2
    3
    4
    5
    
    Now the ArrayList elements are: 
    80000
    2
    3
    4
    5
    

    输出

    显示 Iterator 和 ListIterator 之间差异的表

    Iterator ListIterator
    Can traverse elements present in Collection only in the forward direction. Can traverse elements present in Collection both in forward and backward directions.
    Helps to traverse Map, List and Set. Can only traverse List and not the other two.
    Indexes cannot be obtained by using Iterator. It has methods like nextIndex() and previousIndex() to obtain indexes of elements at any time while traversing List.
    Cannot modify or replace elements present in Collection We can modify or replace elements with the help of set(E e)
    Cannot add elements and it throws ConcurrentModificationException. Can easily add elements to a collection at any time.
    Certain methods of Iterator are next(), remove() and hasNext(). Certain methods of ListIterator are next(), previous(), hasNext(), hasPrevious(), add(E e).