LinedList源码解析

LinedList

底层是链表:增删快、查询慢,且LinedList没有实现线程安全机制。

继承结构:

实现的接口:

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    // 实际元素个数
    transient int size = 0;
    // 头结点
    transient Node<E> first;
    // 尾结点
    transient Node<E> last;
}

1)List接口:列表,add、set、等一些对列表进行操作的方法

2)Deque接口:有队列的各种特性,

3)Cloneable接口:能够复制,使用那个copy方法。

4)Serializable接口:能够序列化。

5)应该注意到没有RandomAccess:那么就推荐使用iterator,在其中就有一个foreach,增强的for循环,其中原理也就是iterator,我们在使用的时候,使用foreach或者iterator都可以。

构造方法:

public LinkedList(Collection<? extends E> c) {
    this();//调用无参构造
    addAll(c);// 添加集合中所有的元素
}
//无参构造
public LinkedList() {
}

内部类:节点类

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

核心方法:

add方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QYawoglv-1606361830746)(java笔记/images/image-20200928113354660.png)]

1.boolean add(E e)

//默认添加是尾插
public boolean add(E e) {
    linkLast(e);
    return true;
}

2.void add(int index, E element)

//根据下标添加
public void add(int index, E element) {
    checkPositionIndex(index);//检查范围

    if (index == size)
        linkLast(element);
    else
        linkBefore(element, node(index));
}

3.addAll

public boolean addAll(Collection<? extends E> c) {
    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<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;
}

4.addFirst和addLast

//头插添加
public void addFirst(E e) {
    linkFirst(e);
}
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++;
}

//尾插添加
public void addLast(E e) {
linkLast(e);
}
void linkLast(E e) {
    final Node<E> l = last;//保存last节点副本
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

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

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

get(index)方法

public E get(int index) {//没有实际的下标,但是会根据链表节点数查询
    checkElementIndex(index);
    return node(index).item;
}

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

遍历方式:

fori方式、foreach方式以及Iterator方式遍历

总结:

1)linkedList本质上是一个双向链表,通过一个Node内部类实现的这种链表结构。
2)linkedList能存储null值。
3)LinkedList在删除和增加等操作上性能好,而ArrayList在查询的性能上好。
4)从源码中看,它不存在容量不足的情况。
5)linkedList不光能够向前迭代,还能像后迭代,并且在迭代的过程中,可以修改值、添加值、还能移除值。
6)linkedList不光能当链表,还能当队列使用,这个就是因为实现了Deque接口。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值