LInkedList源码分析

来源:互联网 发布:航天信息开票软件下载 编辑:程序博客网 时间:2024/06/06 21:39

Linkedlist源码分析

LinkedList定义

LinkedList是一个继承于AbstractSequentialList的双向循环列表。它也可以被当作堆栈。队列或双端队列进行操作。
LinkedList 实现 List 接口,能对它进行队列操作。
LinkedList 实现Deque接口,即能将LinkedList当作双端队列使用。
LinkedList 实现了Cloneable接口,即覆盖了函数clone(),能克隆。
LinkedList 实现java.io.Serializable接口,这意味着LinkedList支持序列化,能通过序列化去传输。

文/嘟嘟MD(简书作者)
原文链接:http://www.jianshu.com/p/681802a00cdf
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

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

LinkedList构造函数

API构造方法摘要

这里写图片描述

/**     * 构造一个空列表。     */    public LinkedList() {    }    /**    构造一个包含指定集合中的元素的列表,这些元素按其集合的迭代器返回的顺序排列。    参数:          c - 要将其元素放到此集合中的列表。     抛出:           NullPointerException - 如果指定的集合为 null。     */    public LinkedList(Collection<? extends E> c) {        this();        addAll(c);    }

LinkedList方法

API方法摘要

这里写图片描述
这里写图片描述

详细方法

●getFirst()

返回此列表的第一个元素。

/**    异常:         NoSuchElementException - 如果此列表为空。     */    public E getFirst() {        final Node<E> f = first;        if (f == null)            throw new NoSuchElementException();        return f.item;    }

●getLast()

返回此列表的最后一个元素。

●removeFirst()

删除并返回此列表的第一个元素。

/**    异常:         NoSuchElementException - 如果此列表为空。     */    public E removeFirst() {        final Node<E> f = first;        if (f == null)            throw new NoSuchElementException();        return unlinkFirst(f);    }

●removeLast()

删除除并返回此列表的最后一个元素。

●void add(int index, E element)

在此列表中指定的位置插入指定的元素。移动当前在该位置处的元素(如果有),所有后续元素都向右移(在其索引中添加 1)。

/**    参数:            index - 要在其中插入指定元素的索引。            element - 要插入的元素。     异常:            如果指定的索引超出范围 (index < 0 || index > size())。抛出checkPositionIndex(index);     */    public void add(int index, E element) {        checkPositionIndex(index);        if (index == size)            linkLast(element);        else            linkBefore(element, node(index));    }

●void addFirst(E o)

将给定元素插入此列表的开头。

●void addLast(E o)

将给定元素追加到此列表的结尾。

●boolean contains(Object o)

如果此列表包含指定元素,则返回 true。

/**    参数:         o - 要测试在此列表中是否存在的元素。     返回:         如果此列表包含指定元素,则返回 true。     */    public boolean contains(Object o) {        return indexOf(o) != -1;    }

●int size()

返回此列表的元素数。

●boolean add(E o)

将指定元素追加到此列表的结尾。

public boolean add(E e) {        linkLast(e);        return true;    }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++;    }

●boolean remove(Object o)

移除此列表中首次出现的指定元素。如果列表不包含该元素,则不作更改。

/**    参数:          o - 要从此列表删除的元素,如果存在。     返回:          如果列表包含指定元素,则返回 true。     */    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;    }    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;    }

●boolean addAll(int index,Collection<__?__extends E> c)

将指定集合中的所有元素从指定位置开始插入此列表。移动当前在该位置上的元素(如果有),所有后续元素都向右移(增加其索引)。新元素将按由指定集合的迭代器返回的顺序在列表中显示。

/**    参数:         index - 在其中插入指定集合中第一个元素的索引。         c - 要插入此列表的元素。     返回:         如果此列表由于调用而更改,则返回 true。     抛出:          IndexOutOfBoundsException - 如果指定的索引超出范围 (index < 0 || index > size())。          NullPointerException - 如果指定的集合为 null     */    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;

●boolean addAll(Collection<__?__extends E> c)

追加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序。

●void clear()

从此列表中移除所有元素。

    public void clear() {        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++;    }

●E set(int index, E element)

将此列表中指定位置的元素替换为指定的元素。

/**    参数:         index - 要替换的元素的索引。         element - 要在指定位置存储的元素。     返回:         以前在指定位置的元素。     抛出:          IndexOutOfBoundsException - 如果指定的索引超出范围 (index < 0 || index >= size())。     */    public E set(int index, E element) {        checkElementIndex(index);        Node<E> x = node(index);        E oldVal = x.item;        x.item = element;        return oldVal;    }

●E remove()

找到并移除此列表的头(第一个元素)。

●E remove(int index)

移除此列表中指定位置处的元素。将任何后续元素向左移(从索引中减1)。返回从列表中删除的元素。

/**     参数:          index - 要删除的元素的索引。     返回:          以前在指定位置的元素。     抛出:            IndexOutOfBoundsException - 如果指定的索引超出范围 (index < 0 || index >= size())。     */    public E remove(int index) {        checkElementIndex(index);        return unlink(node(index));    }    unlink函数请参见上面remove(Object o)方法。

●int indexOf(Object o)

返回此列表中首次出现的指定元素的索引,如果列表中不包含此元素,则返回 -1。

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

● E peek()、 E element()

找到但不移除此列表的头(第一个元素)。

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

● E poll()、 E remove()

找到并移除此列表的头(第一个元素)。

public E poll() {        final Node<E> f = first;        return (f == null) ? null : unlinkFirst(f);    }public E remove() {        return removeFirst();    }

● ListIterator listIterator(int index)

返回此列表中的元素的列表迭代器(按适当顺序),从列表中指定位置开始。遵守 List.listIterator(int) 的常规协定。

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

● Object clone()

返回此 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;    }

● Object[] toArray()

以正确顺序返回包含此列表中所有元素的数组、

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

● public T[] toArray(T[] a)

以正确顺序返回包含此列表中所有元素的数组;返回数组的运行时类型即为指定数组的类型。

/**    参数:         a - 要在其中存储列表元素的数组(如果它足够大);否则,为其分配具有相同运行时类型的新数组。     抛出:          ArrayStoreException - 如果 a 的运行时类型不是此列表中每个元素的运行时类型的超类型。          NullPointerException - 如果指定的数组为 null。     */    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;    }
0 0
原创粉丝点击