Java集合002 --- LinkedList源码解析
2021-01-17 04:14
                         标签:throw   red   批量添加   efault   object   返回值   swa   区别   else    前言 LinkedList内部实现是一个双链表,linkedList除了实现了list相关的接口外,还实现了Queue、Dequeue接口,所以它有着双端队列、list、栈的功能      注意LinkedList没有实现RandomAccess接口,这意味着LinkedList没有提供快速随机访问功能 属性     构造器     添加单个元素方法 1、添加单个元素到尾部(返回值boolean类型,表示是否添加成功) 2、在指定索引添加一个元素 删除单个元素方法     两个集合的并集     作为栈的push和pop方法 push为入栈,pop为出栈     peek和element方法  功能都是获取首节点元素,区别是peek在首节点为null时,返回null;而element方法在首节点为null时抛出NoSuchElementException异常 poll和remove方法 功能都是删除首节点,区别是poll在首节点为null时,返回null;而remove方法在首节点为null时抛出NoSuchElementException异常 序列化和反序列化 和ArrayList类似,LinkedList序列化方式也是通过提供私有无返回值的writeObject和readObject方法,按需序列化   Java集合002 --- LinkedList源码解析 标签:throw   red   批量添加   efault   object   返回值   swa   区别   else    原文地址:https://www.cnblogs.com/sniffs/p/12919791.html
// 链表数据长度
transient int size = 0;
// 链表首指针
transient Node
private static class Node
    E item;
    Node
    Node
    Node(Node
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}public LinkedList() {
}
public LinkedList(Collection extends E> c) {
    this();
    addAll(c);
}
public boolean addAll(Collection extends E> c) {
    // 复用了在指定索引批量添加元素的方法, 只不过这里size在构造器调用时为0
    return addAll(size, c);
}
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
public boolean add(E e) {
    linkLast(e);
    return true;
}
void linkLast(E e) {
    final Node
    // 因为是在尾部添加节点, 所以新节点前一个节点为last, 后一个节点为null
    final Node
    last = newNode;// 新节点置为尾结点S
    if (l == null) // 双链表为空, 新节点即为首节点
        first = newNode;
    else
        l.next = newNode; // 否则将原来尾结点的next置为新节点
    size++; // 长度加一
    modCount++; // 序列化时使用
}public void add(int index, E element) {
    // 校验索引, 为啥这里索引可以取值为size呢?这是因为可以在尾部添加节点
    checkPositionIndex(index);
    if (index == size)
        linkLast(element); // 尾部添加节点, 同上
    else
        linkBefore(element, node(index));// 指定索引添加节点
}
private void checkPositionIndex(int index) {
    if (!isPositionIndex(index))
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
    return index >= 0 && index  size;
}
Node
public boolean remove(Object o) {
    if (o == null) {
        for (Node
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

public E peek() {
    final Node
public E poll() {
    final Node
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
上一篇:什么是线程安全,怎样保证线程安全
下一篇:ES6 —— 数组
文章标题:Java集合002 --- LinkedList源码解析
文章链接:http://soscw.com/index.php/essay/43027.html