jdk1-8-0-73源码解读——LinkedList实现

1.1 LinkedList简介

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

1.2 类图

替代文字

1.3 成员变量和类

transient int size = 0; 
transient Node<E> first; 
transient Node<E> last; 

总共就三个变量,size表示大小,first表示指向第一个节点的引用,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;
    }
}

1.4 带参构造器

 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; //定义两个node
        if (index == size) { //如果插入的位置恰好为size
            succ = null;  
            pred = last;   //pred定义为链表的最后一个节点
        } else { //如果index小于size
            succ = node(index); //succ赋值为index处节点 
            pred = succ.prev;  //pred为succ的前一个节点
        }

        for (Object o : a) { //进行foreach遍历
            @SuppressWarnings("unchecked") E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null); //定义新的节点
            if (pred == null) //如果pred为空则表示当前链表为空
                first = newNode; //赋值first
            else //如果链表不为空
                pred.next = newNode; //维护pred后置节点
            pred = newNode; 
        }

        if (succ == null) { //表示当前只有一个节点
            last = pred; //first,last都是pred
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;  //改变链表大小
        modCount++; //记录变更次数,父类属性,对当前链表的增删改都需要维护该属性
        return true;
    }

1.5 常用方法

//新增
 public boolean add(E e) {
        linkLast(e);
        return true;
    }
//这里使用的是linklast(e),下面是该方法源码:
void linkLast(E e) {
      final Node<E> l = last; //存储last节点
      final Node<E> newNode = new Node<>(l, e, null);
      last = newNode; //维护last节点
      if (l == null) //当前链表为空
         first = newNode; 
      else //维护后置节点
         l.next = newNode;
      size++; //维护size属性
      modCount++; //记录变更次数
    }
//移除   
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(x)作用是删除非空节点,下面看一下该方法的源码:
E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item; //item表示实际的对象
        final Node<E> next = x.next; //当前x的前置节点
        final Node<E> prev = x.prev; //当前x的后置节点

        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;  //将x.item引用赋值为null
        size--; //维护size
        modCount++;
        return element;
    }

有问题或建议请联系邮箱:981017952@qq.com,qq:981017952

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值