容器-LinkedList源码分析

 

概述

在之前的集合框架的文章中,知道了ArrayList和LinkedList是List接口的两种不同实现。也明白了ArrayList和LinkedList的区别。

ArrayList和LinkedList的区别:

1. 是否保证线程安全: ArrayList 和 LinkedList 都是不同步的,也就是不保证线程安全;

2. 底层数据结构: ArrayList 底层使用的是 Object 数组;LinkedList 底层使用的是 双向链表 数据结构

3. 插入和删除是否受元素位置的影响: ① ArrayList 采用数组存储,所以插入和删除元素的时间复杂度受元素位置的影响。 比如:执行add(E e)方法的时候, ArrayList 会默认在将指定的元素追加到此列表的末尾,这种情况时间复杂度就是O(1)。但是如果要在指定位置 i 插入和删除元素的话(add(int index, E element))时间复杂度就为 O(n-i)。因为在进行上述操作的时候集合中第 i 和第 i 个元素之后的(n-i)个元素都要执行向后位/向前移一位的操作。 ② LinkedList 采用链表存储,所以插入,删除元素时间复杂度不受元素位置的影响,都是近似 O(1)而数组为近似 O(n)。

4. 是否支持快速随机访问: LinkedList 不支持高效的随机元素访问,而 ArrayList 支持。快速随机访问就是通过元素的序号快速获取元素对象(对应于get(int index)方法)。

5. 内存空间占用: ArrayList的空 间浪费主要体现在在list列表的结尾会预留一定的容量空间,而LinkedList的空间花费则体现在它的每一个元素都需要消耗比ArrayList更多的空间(因为要存放直接后继和直接前驱以及数据)。

下来重点讲述下LinkedList:

LinkedList是线程不安全,允许元素为null的双向链表。其底层结构为链表,实现了List<E>,Deque<E>,Cloneable,java.io.Serilizable接口。因为实现了Deque<E>,所以也可以作为一个双端队列。

LinkedList源码分析

1.LinkedList的继承和层次关系

下面是LinkedList的UML图:

è¿éåå¾çæè¿°

(1)继承AbstractSequentialList抽象类(AbstractSequentialList 继承自 Abstract,只知道按次序访问):在对LinkedList进行遍历时,更推荐使用迭代器访问。(虽然LinkedList提供了ger(int index)方法,但是在使用这个方法时,都需要从链表首部或尾部开始遍历,每次的遍历时间复杂度为o(index),所以不推荐这个方法。)

(2)实现了List接口。

(3)实现了Cloneablej接口,支持浅克隆。LinkedList的节点没有被克隆,只是通过Object的clone()方法得到了Object对象强制转换成LinkedList,然后把它的内部实例域置空,把LinkedList节点中每一个值都拷贝到clone中。

(4)实现了Deque接口。

(5)实现了Serializable接口,表明它支持序列化。

2.LinkedList的数据结构

private static class Node<E> {
        E item;//节点的值
        Node<E> next;//当前节点的后一个节点的引用(指向当前节点后一个节点的指针)
        Node<E> prev;//当前节点的前一个节点的引用(指向当前节点的前一个节点的指针)

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

Node类是LinkedList中的私有内部类,LinkedList就是通过Node类来存储集合中的元素。

LinkedList中的元素:

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
    {
    transient int size = 0;   //用来记录LinkedList的大小

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;    //用来表示LinkedList的头节点

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;    //用来表示LinkedList的尾节点
 }

3.LinkedList的构造方法

LinkedList提供了两个构造器,ArrayList多提供一个通过设置初始化容量来初始化类。LinkedList不提供的原因:因为LinkedList底层是通过链表实现的,每当有新元素添加进来的时候,都是通过链表的新节点实现的,也就是说LinkedList的容量是随着元素的个数的变化而动态变化的。而ArrayList底层是通过数组来存储新添加的元素,所以可以为ArrayList设置初始容量(实际设置数组的大小)。

(1)空构造方法:

 /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }

(2)有参构造方法:用已有的集合创建链表

 /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param  c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

4.核心方法

4.1 添加方法

(1) boolean add(E e);将元素添加到链表尾部。

/**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

调用了linkLast方法。

void linkLast(E e);链接使e成为最后一个元素

     /**
     * Links e as last element.
     */
    void linkLast(E e) {
        //当前链表的尾节点(这个尾结点有尾指针和指向前面的前指针)
        final Node<E> l = last;  
        //创建新的一个节点newNode,
        //参数l(尾结点的前指针)作newNode的前指针,e作为newNode的节点值,newNode尾指针指向null
        final Node<E> newNode = new Node<>(l, e, null); 
         //newNode成为尾结点
        last = newNode;
       //如果链表为空的话,新的节点就是头结点
       //不空的话,让尾指针指向新的节点
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

(2)add(int index,E e):在指定位置添加元素

   
    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

首先调用checkPositionIndex方法,判断插入位置是否合理。如果插入的位置刚好是链表末尾,就可以采用上面的linkLast方法。如果没有在末尾,用node方法找到index位置的节点,让这个节点作为参数传入linkBefore方法,调用linkBefore方法让节点element插入到index位置的前面。

void checkPositionIndex(int index);用于判断当我们新添加元素的时候,传进来的index是否合法。

private void checkPositionIndex(int index) {
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

调用了isPositionIndex方法,新添加的元素可以在链表的末尾,所以index可以<=size。

private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }

Node<E> node(int index);查找并返回index位置的节点。

     /**
     * Returns the (non-null) Node at the specified element index.
     */
    Node<E> node(int index) {
        // assert isElementIndex(index);
        //链表没有head
        //size为链表的大小,让size向右移动一位,就是让size除2
        //index小于链表的一半,说明在链表的前一半
        //从链表的前面开始查找,减少了时间复杂度
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
           //否则从链表尾部开始查找,用前指针
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

void linkBefore(E e, Node<E> succ);将节点e插入到非空节点succ的前面。

    /**
     * Inserts element e before non-null Node succ.
     */
    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        //节点pred为succ的前节点
        final Node<E> pred = succ.prev;
        //创建一个新节点newNode
        //newNode的前节点指针指向pred,后指针执行succ,指针值为succ
        final Node<E> newNode = new Node<>(pred, e, succ);
        //succ的前指针执行newNode
        succ.prev = newNode;
        //如果前指针为空,newNode为新节点
        //否则将前节点的后指针指向newNode
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
       

(3)addAll(Collection c );将集合插入到链表尾部。(因为传入的位置参数是size,所以从链表尾部插入。)

public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

(4)addAll(int index, Collection c); 将集合从指定位置开始插入

public boolean addAll(int index, Collection<? extends E> c) {
        //1:调用checkPositionIndex方法(上面介绍过的)检查index范围是否在size之内
        checkPositionIndex(index);

        //2:toArray()方法把集合的数据存到对象数组中
        //就是指:先把集合转换成数组,然后为该数组添加一个新的引用(Object[])
        Object[] a = c.toArray();
        //新建一个变量存储数组的长度
        int numNew = a.length;
        //数组为空,直接返回
        if (numNew == 0)
            return false;

        //3:得到插入位置的前驱节点和后继节点
        //创建两个节点pred,succ
        //pred:插入后的前驱节点
        //succ: 插入后的后继节点
        Node<E> pred, succ;
        //如果插入位置为尾部,前驱节点为last,后继节点为null
        if (index == size) {
            succ = null;
            pred = last;
        }
        //否则,调用node()方法得到该位置的节点(也就是插入后的后继节点),再得到前驱节点
        else {
            succ = node(index);
            pred = succ.prev;
        }

        // 4:遍历数据将数据插入
        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            //创建新节点
            Node<E> newNode = new Node<>(pred, e, null);
            //如果插入位置在链表头部
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        //如果插入位置在尾部,重置last节点
        if (succ == null) {
            last = pred;
        }
        //否则,将插入的链表与先前链表连接起来
        else {
            pred.next = succ;
            succ.prev = pred;
        }
        //插入成功后,链表的大小需要改正。
        size += numNew;
        modCount++;
        return true;
    }    

插入的过程如图所示:

上面可以看出addAll方法通常包括下面四个步骤:

  • 检查index范围是否在size之内
  • toArray()方法把集合的数据存到对象数组中
  • 得到插入位置的前驱和后继节点
  • 遍历数据,将数据插入到指定位置

(5)addFirst(E e); 将元素添加到链表头部。

 public void addFirst(E e) {
        linkFirst(e);
    }

调用了linkFirst方法。

void linkFirst(E e);把参数中的元素作为链表的第一个元素。

private void linkFirst(E e) {
        //新建一个节点,将头节点存储起来
        final Node<E> f = first;
        //新建节点,以头节点为后继节点,前驱节点为null
        final Node<E> newNode = new Node<>(null, e, f);
        //头节点设为新建节点
        first = newNode;
        //如果链表为空,last节点也指向该节点
        if (f == null)
            last = newNode;
        //否则,将原先头节点的前驱指针指向新节点,也就是指向前一个元素
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

(6)addLast(E e);将元素添加到链表尾部。

public void addLast(E e) {
        linkLast(e);
    }

调用了linkLast方法。

void linkLast(E e);把参数中的元素作为链表的最后一个元素。

private void linkLast(E e) {
        //新建一个节点将尾节点存储起来
        final Node<E> l = last;
      //创建一个新的节点,其前驱指针指向last,后继指针为null
        final Node<E> newNode = new Node<>(l, e, null);
        //尾节点为新建节点
        last = newNode;
//再判断一下l是否为空,如果为空的话,说明原来的LinkedList为空,所以同时也需要把新节点设为头节点
        if (l == null)
            first = newNode;
    //否则原尾节点l的next设置为newNode。
        else
            l.next = newNode;
        size++;
        modCount++;
    }

(7)(实现双端队列接口的方法)boolean offerFirst(E e);在链表的头部添加一个元素。

       boolean offerLast(E e);在链表的尾部添加一个元素。

 public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }
 public boolean offerLast(E e) {
        addLast(e);
        return true;
    }

 

4.2 删除方法

(1) remove() ,removeFirst(),pop(): 删除头节点

public E pop() {
        return removeFirst();
    }
public E remove() {
        return removeFirst();
    }
public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

remove() ,removeFirst()两个方法都调用了removeFirst方法。

E removeFirst();删除该链表的头节点,并返回删除值。

public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

上述的删除方法都直接后间接调用了 unlinkFirst方法。

E unlinkFirst(Node<E> f);删除头节点。

private E unlinkFirst(Node<E> f) {
      //官方文档的代码中也给出了注释:使用该方法的前提是参数f是头节点,而且f不能为空。
        // assert f == first && f != null;
         //定义一个变量element指向待删除节点的值
        final E element = f.item;
        //定义一个节点next指向待删除节点的下一个节点(这个节点将会成为头节点)
        final Node<E> next = f.next;
        //将待删除节点的节点值设置为0
        f.item = null;
        //待删除节点的next指针指向空
        f.next = null; // help GC
        //将链表的头节点设置为next(原本头节点的下一个节点)
        first = next;
        //如果原来的next(现在的头节点)为空,将last指向空
        if (next == null)
            last = null;
         //否则的话,next为头节点,前驱指针执行为空
        else
            next.prev = null;
        //链表的大小需要改正
        size--;
        modCount++;
        return element;
    }

(2)removeLast(),pollLast(): 删除尾节点

public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }
public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }

上述的删除方法都直接后间接调用了 unlinkLast方法。

unlinkLast(Node<E> l);删除最后一个节点,并返回删除值。

//基本方式与unlinkFirst相似
private E unlinkLast(Node<E> l) {
        // assert l == last && l != null;
        final E element = l.item;
        //新建一个节点prex,指向待删除节点的前一个节点(这个节点会成为链表的尾节点)
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; // help GC
        //尾节点设置为prev
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }

(3)remove(Object o): 删除指定元素。不过该方法一次只会删除一个匹配的对象,如果删除了匹配对象,返回true,否则false。

public boolean remove(Object o) {
        //如果删除对象为null
        if (o == null) {
            //从头开始遍历
            for (Node<E> x = first; x != null; x = x.next) {
                //找到元素
                if (x.item == null) {
                   //从链表中移除找到的元素
                    unlink(x);
                    return true;
                }
            }
        } else {
            //从头开始遍历
            for (Node<E> x = first; x != null; x = x.next) {
                //找到元素
                if (o.equals(x.item)) {
                    //从链表中移除找到的元素
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

E unlink(Node x) 方法:

E unlink(Node<E> x) {
        // assert x != null;
        //创建三个变量
        //第一个变量用来存储待删除节点的值
        final E element = x.item;
        //第二个变量是个节点,用来指向待删除节点的后继节点
        final Node<E> next = x.next;
        //第三个变量是个节点,用来指向待删除节点的前驱节点
        final Node<E> prev = x.prev;

        //删除前驱指针
        //如果前驱节点为空,说明待删除的节点是头节点,设置头节点为该节点的后继节点next
        if (prev == null) {
            first = next;
        } else {   //否则,让前驱节点的后继指针指向后继节点,断掉指向待删除节点的指针 
            prev.next = next;
             //删除前驱指针
            x.prev = null;
        }

        //删除后继指针
         //如果后继节点为空,说明待删除节点是尾节点,设置尾节点为该节点的前驱节点prev
        if (next == null) {
            last = prev;
        } else {
        //否则的话待删除节点的下一个节点的前驱指针指向待删除的前驱节点
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }

删除的过程如图所示:

(4)remove(int index):删除指定位置的元素。

public E remove(int index) {
        //检查index范围
        checkElementIndex(index);
        //将节点删除
        return unlink(node(index));
    }

(5)boolean removeFirstOccurrence(Object o);删除第一次在链表中出现的o。

boolean removeLastOccurrence(Object o);删除最后出现在链表中的o。

//第一次出现的从前向后
public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }
//最后出现的从后向前
 public boolean removeLastOccurrence(Object o) {
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

4.3 根据位置取数据的方法

(1)get(int index): 根据指定索引返回数据

public E get(int index) {
        //检查index范围是否在size之内
        checkElementIndex(index);
        //调用Node(index)去找到index对应的node然后返回它的值
        return node(index).item;
    }

(2)获取头节点(index=0)数据方法:(实现双端队列接口的方法)

public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }
public E element() {
        return getFirst();
    }
public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

    //与上面的方法不同的是,获得了头节点的值,但是在链表中也删除了头节点
    public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

getFirst(),element(),peek(),peekFirst() 这四个获取头结点方法的区别在于对链表为空时的处理,是抛出异常还是返回null,其中getFirst() 和element() 方法将会在链表为空时,抛出异常。element()方法的内部就是使用getFirst()实现的。它们会在链表为空时,抛出NoSuchElementException。

(3)获取尾节点(index=-1)数据方法:(实现双端队列接口的方法)

 public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }
 public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

两者区别: getLast() 方法在链表为空时,会抛出NoSuchElementException,而peekLast() 则不会,只是会返回 null

4.4 根据对象得到索引的方法

(1)int indexOf(Object o): 从头遍历找。

//有两中情况,参数o为空和不空
public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            //从头遍历
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            //从头遍历
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }

(2)int lastIndexOf(Object o): 从尾遍历找

//和从头遍历一样,也有两种情况,o为空和不空
public int lastIndexOf(Object o) {
        int index = size;
        if (o == null) {
            //从尾遍历
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (x.item == null)
                    return index;
            }
        } else {
            //从尾遍历
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (o.equals(x.item))
                    return index;
            }
        }
        return -1;
    }

4.5 检查链表是否包含某对象的方法

contains(Object o): 检查对象o是否存在于链表中

 public boolean contains(Object o) {
       //调用indexOf方法,通过这个方法根据对象查找索引
        return indexOf(o) != -1;
    }

4.6 计算链表的大小

int size();直接返回实例域size。

 public int size() {
        return size;
    }

4.7 清除链表的所有元素

void clear();

  public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        //遍历链表,将每个节点置空
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
       //将头节点和尾节点置空
        first = last = null;
        size = 0;
        modCount++;
    }

4.8 设置对应索引的节点的值

E set(int index, E element);设置对应索引的节点的值,并返回原来的取值。

public E set(int index, E element) {
       //要判断索引是否合法
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

4.9 返回一个越界信息

String outOfBoundsMsg(int index);

private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

4.10 克隆

Object clone() ;调用superClone方法获得LinkedList对象,把该对象的所有域设为初始值,然后把LinkedList的所有内容全都复制到返回数组中。

public Object clone() {
        LinkedList<E> clone = superClone();

        // Put clone into "virgin" state
        clone.first = clone.last = null;
        clone.size = 0;
        clone.modCount = 0;

        // Initialize clone with our elements
        for (Node<E> x = first; x != null; x = x.next)
            clone.add(x.item);

        return clone;
    }

调用了superClone方法。

 LinkedList<E> superClone();直接调用了超类的clone()方法,然后把得到的Object对象转换成为LinkeList类型。

 @SuppressWarnings("unchecked")
    private LinkedList<E> superClone() {
        try {
            return (LinkedList<E>) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e);
        }
    }

4.11 转换成数组

(1)没有参数

//通过遍历 
 public Object[] toArray() {
        Object[] result = new Object[size];
        int i = 0;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;
        return result;
    }

(2)有参数:参数为数组。


     //泛型方法
    public <T> T[] toArray(T[] a) 
       //在方法内部,如果a的长度小于集合的大小的话
        if (a.length < size)    
     //通过反射创建一个和集合同样大小的数组
            a = (T[])java.lang.reflect.Array.newInstance(
                                a.getClass().getComponentType(), size);
        int i = 0;
        Object[] result = a;
        //接着把集合中的所有元素添加到数组中
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;
        //如果数组的元素的个数大于集合中元素的个数,则把a[size]设置为空。
        if (a.length > size)
            a[size] = null;

        return a;
    }

4.12 序列化和反序列化

和ArrayList一样,LinkedList实现序列化和反序列化是通过writeObject和readObject方法。

//将LinkeList状态保存在流中,序列化
private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {
        // Write out any hidden serialization magic
        s.defaultWriteObject();

        // Write out size
        s.writeInt(size);

        // Write out all elements in the proper order.
        for (Node<E> x = first; x != null; x = x.next)
            s.writeObject(x.item);
    }
//从流中把LinkedList读出来
@SuppressWarnings("unchecked")
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        // Read in any hidden serialization magic
        s.defaultReadObject();

        // Read in size
        int size = s.readInt();

        // Read in all elements in the proper order.
        for (int i = 0; i < size; i++)
            linkLast((E)s.readObject());
    }

4.13 迭代器(两种)

提供两种迭代器(和ArrayList一样)。

(1)返回Iterator迭代器;(向后遍历)


    public Iterator<E> descendingIterator() {
        return new DescendingIterator();
    }

  
    private class DescendingIterator implements Iterator<E> {
        private final ListItr itr = new ListItr(size());
        public boolean hasNext() {
            return itr.hasPrevious();
        }
        public E next() {
            return itr.previous();
        }
        public void remove() {
            itr.remove();
        }
    }

(2)返回ListIterator迭代器。

 public ListIterator<E> listIterator(int index) {
        checkPositionIndex(index);
        return new ListItr(index);
    }

注意:因为ArrayList和LinkedList都直接或间接继承了Abstract类,在这个类中定义了两个迭代器。一个是实现了Iterator接口的迭代器Itr,另一个是实现了ListIterator并且继承自Itr的迭代器ListItr。 所以这两个迭代器在ArrayList和LinkedList中相同。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值