集合系列LinkedList

#基于jdk1.8# 

1、LinkedList是一种链表结构。

2、first节点和last节点作为list单独作为属性存在的。默认的构造方法是一个空的list。这里有别于jdk1.6(1.7没做过研究)1.6中默认存在一个null的header节点。默认构造方法new了过后pre=next=header。

transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;


    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;


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

2、LinkedList内部存在一个静态内部类,链表的每个节点就是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;
        }
    }

3、我们再来分析默认的构造方法。因为默认构造方法只创建了一个空的list。所以初始状态下first和last都是null。

4.我们再看add方法,主要调用的是linkLast(E e)方法。注意观察调用Node构造方法时next传入的是null。(这里也是有别于jdk1.6的,个人认为这么设计的目的是为了节约内存的消耗)最后新的这个node节点是添加在list末尾的。且这个node节点的next节点是null。但是last被指向到当前的node。如果这是第一个添加的节点那first也指向当前node。如果不是第一个节点上一个节点的next指向当前node。

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

 /**
     * Links e as last element.
     */
    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++;
    }

5、再看remove内部实际调用removeFirst()方法。removeFirst实际调用unlinkFirst(Node<E> f) 方法。调用unlinkFirst方法时传入first作为参数。可见remove移掉的是第一个节点。将first节点的next节点作为最新的first节点。且将next节点的pre节点置为null。

 /**
     * Retrieves and removes the head (first element) of this list.
     *
     * @return the head of this list
     * @throws NoSuchElementException if this list is empty
     * @since 1.5
     */
    public E remove() {
        return removeFirst();
    }

 /**
     * Removes and returns the first element from this list.
     *
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }


 /**
     * Unlinks non-null first node f.
     */
    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;
    }

6、再看get方法。核心代码在于node(int index)方法。先定位传入的下标处于整个list的上半部分还是下半部分。如果在上半部分直接从first开始遍历,直到找到对应下标的值再返回。如果处于下半部分直接从last开始遍历。

 /**
     * Returns the element at the specified position in this list.
     *
     * @param index index of the element to return
     * @return the element at the specified position in this list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

 /**
     * Returns the (non-null) Node at the specified element index.
     */
    Node<E> node(int index) {
        // assert isElementIndex(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;
        }
    }

7、linkedList同样重写了writeObject和readObject。原因见上一篇ArrayList文章中说明。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值