LinkedList源码分析(1.7.0_80)

来源:互联网 发布:宝宝模样合成软件 编辑:程序博客网 时间:2024/06/05 00:34

定义

public class LinkedList<E>    extends AbstractSequentialList<E>    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

AbstractSequentialList:功能主要继承者,主要实现依靠ListIterator,ListIterator继承Iterator,LinkedList中需要实现ListIterator中的方法,避免父类复杂度。
Deque:双向列表继承
不支持同步修改:主要依靠modCount即fail-fast机制

属性

transient int size = 0; 大小
transient Node first; 首节点
transient Node last;末节点
Node类代码如下:

    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;        }    }

定义前中后三个对象

初始化

   public LinkedList() {    }
  public LinkedList(Collection<? extends E> c) {        this();        addAll(c);    }

主要方法

linkFirst

private void linkFirst(E e) {        final Node<E> f = first;        final Node<E> newNode = new Node<>(null, e, f);        first = newNode;        if (f == null)            last = newNode;        else            f.prev = newNode;        size++;        modCount++;    }

作用:将e作为第一个元素
解析:首先获取原第一节点f,构建newNode,此节点前元素为null,中为e,后元素为f,将newNode作为第一个节点,若f为空则LinkedList只有newNode一个节点,否则原第一个节点f的前元素指向newNode。

linkLast

  void linkLast(E e) {        final Node<E> l = last;        final Node<E> newNode = new Node<>(l, e, null);        last = newNode;        if (l == null)            first = newNode;        else            l.next = newNode;        size++;        modCount++;    }

作用:将e作为最后一个元素。
解析:大致原理同linkFirst相同,构建的newNode 后元素为null,区别两者用不同的访问级别private和protected,暂未明白其中原理,两者都不能被外界调用。

linkBefore

 void linkBefore(E e, Node<E> succ) {        // assert succ != null;        final Node<E> pred = succ.prev;        final Node<E> newNode = new Node<>(pred, e, succ);        succ.prev = newNode;        if (pred == null)            first = newNode;        else            pred.next = newNode;        size++;        modCount++;    }

作用:在一个节点前插入元素e
解析:获得当前节点的前元素,构建newNode ,其中e的后元素为succ,将succ原前元素指向newNode ,e的前元素为pred,原pred的后元素还是指向succ节点,所以pred.next = newNode;若pred 为null则succ为first节点,最好画图理解。

unlinkFirst

private E unlinkFirst(Node<E> f) {        // assert f == first && f != null;        final E element = f.item;        final Node<E> next = f.next;        f.item = null;        f.next = null; // help GC        first = next;        if (next == null)            last = null;        else            next.prev = null;        size--;        modCount++;        return element;    }

作用:去除第一个非空节点
解析:将节点中元素置空,后元素置空,后节点至成first节点,first节点特点,前元素为null,若next为空则LinkedList为空,并返回中元素,外部不可调用

unlinkLast

  private E unlinkLast(Node<E> l) {        // assert l == last && l != null;        final E element = l.item;        final Node<E> prev = l.prev;        l.item = null;        l.prev = null; // help GC        last = prev;        if (prev == null)            first = null;        else            prev.next = null;        size--;        modCount++;        return element;    }

作用:去除最后一个非空节点
解析:原理同unlinkFirst差不多,last特点后元素为null所以prev.next = null;

   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;        if (prev == null) {            first = next;        } else {            prev.next = next;            x.prev = null;        }        if (next == null) {            last = prev;        } else {            next.prev = prev;            x.next = null;        }        x.item = null;        size--;        modCount++;        return element;    }

作用:取出一个非空节点
原理:大致同上相同,prev == null,若x的前节点为空,则x为first节点,x置空后则它的后节点为first节点,next的前节点为x,x置空后为null,next满足作为fist节点要求(前元素为null),由此可知当last,first同时为null时LinkedList为空,同是指向一个节点LinkedList只有一个元素。此方法不可外部调用

getFirst

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

作用:返回first节点中元素

getLast

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

作用:返回last中元素

removeFirst

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

作用:移除第一个节点,返回中元素
解析:参考unlinkFirst

addFirst

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

作用:同linkFirst

addLast

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

作用:同linkLast

contains

public boolean contains(Object o) {        return indexOf(o) != -1;    }

作用:是否包含元素o

indexOf

    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;    }

作用:返回此元素在结合中的第一个下标,若无为-1

add

public boolean add(E e) {        linkLast(e);        return true;    }

作用:在结合末尾添加一个元素

remove

   public boolean remove(Object o) {        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;    }

作用:删除o在集合中第一次出现的位置
解析:参考unlink

addAll(Collection

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

作用:将集合c插入LinkedList中,正序返回

addAll(int index, Collection

    public boolean addAll(int index, Collection<? extends E> c) {        checkPositionIndex(index);        Object[] a = c.toArray();        int numNew = a.length;        if (numNew == 0)            return false;        Node<E> pred, succ;        if (index == size) {            succ = null;            pred = last;        } else {            succ = node(index);            pred = succ.prev;        }        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;        }        if (succ == null) {            last = pred;        } else {            pred.next = succ;            succ.prev = pred;        }        size += numNew;        modCount++;        return true;    }

作用:从指定位置开始将集合c插入到LinkedList中
解析:checkPositionIndex使index最大只能为LinkedList的size,

clone

  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;    }

作用:LinkedList浅拷贝

toArray(T[] a)

  public <T> T[] toArray(T[] 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;        if (a.length > size)            a[size] = null;        return a;    }