LinkedList源码阅读笔记(基于JDK1.8)
public void addFirst(E e) { //添加元素到列表头
linkFirst(e);
}
public void addLast(E e) { //添加元素到列表尾
linkLast(e);
}
public boolean add(E e) { //添加元素到列表头,会返回添加是否成功
linkLast(e);
return true;
}
public void add(int index, E element) { //添加元素到index位置
checkPositionIndex(index); //检查index是否越界
if (index == size) //如果index等于链表长度则插入到链表尾
linkLast(element);
else //否则插入到链表头
linkBefore(element, node(index));
}
public boolean addAll(int index, Collection<? extends E> c) { //在index位置插入集合c的全部元素
checkPositionIndex(index); //检查index是否越界
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) { //如果index为链表长度,则插入位置为链表尾
succ = null;
pred = last;
} else { //否则将记录index位置的前一个元素,为之后连接链表准备
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;
}
//插入完毕,将原集合index之后的部分连接到链表尾
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
public E set(int index, E element) { //修改index位置元素为element
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
public E getFirst() { //获取链表头元素
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
public E getLast() { //获取链表尾元素
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
public E get(int index) { //获取链表index位置的元素
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) { //获取特定位置元素的实现方法
// assert isElementIndex(index);
if (index < (size >> 1)) { //如果index小于链表长度的一半则从前向后遍历前一半链表
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else { //如果index大于等于链表长度的一半则从后向前遍历后一半链表
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
public E removeFirst() { //删除链表头元素,类似pop
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
public E removeLast() { //删除链表尾元素
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
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; // 这两个置为空是为了方便GC
first = next; //第二个元素提置第一位,将原第一位元素踢出链表
if (next == null) //如果没有next说明删除链表头元素后链表为空,将last置为空
last = null;
else //next不为空说明删除后链表还存在元素,将现第一位元素的prev置为空(链表头元素prev为空)
next.prev = null;
size--;
modCount++;
return element;
}
private E unlinkLast(Node<E> l) { //删除链表尾元素的实现方法
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // 这两个置为空是为了方便GC
last = prev; //将倒数第二个元素置为链表尾元素,将原链表尾元素踢出链表
if (prev == null) //如果prev为空则说明删除链表尾元素后链表为空,没有已保存的元素,此时first为空
first = null;
else //如果prev不为空,说明删除之后链表还存在元素,此时需将链表尾元素的next元素置为空(链表尾元素next为空)
prev.next = null;
size--;
modCount++;
return element;
}
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;
}
public E remove(int index) { //删除指定位置的元素
checkElementIndex(index);
return unlink(node(index));
}
public E remove() { //删除链表头元素
return removeFirst();
}
E unlink(Node<E> x) { 删除指定元素实现方法
// assert x != null;
final E element = x.item; //参数元素的value,用于返回值
final Node<E> next = x.next; //参数元素的下一位元素
final Node<E> prev = x.prev; //参数元素的上一位元素
if (prev == null) { //prev为空说明参数元素是链表头元素,将下一位元素提置链表头
first = next;
} else { //将上一位元素和下一位元素相连
prev.next = next;
x.prev = null;
}
if (next == null) { //如果next为空说明参数元素是链表尾元素,将上一位元素置为链表尾
last = prev;
} else { //将上一位元素和下一位元素相连
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
public boolean removeLastOccurrence(Object o) { //找到元素出现的最后位置并删除
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
public E next() { //获取下一个元素
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
public E previous() { //获取上一个元素,需要注意的是当调用next然后调用previous时会返回同一个元素,因为调用next的时候nextIndex被加一,而previous中nextIndex减一,所以会取到同一个元素
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
public void remove() { //实现了fail-fast的删除
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
public void set(E e) { //实现了fail-fast的修改
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) { //实现了fail-fast的新增
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
LinkedList源码阅读笔记(基于JDK1.8)的更多相关文章
- ArrayList源码阅读笔记(基于JDk1.8)
关键常量: private static final int DEFAULT_CAPACITY = 10; 当没有其他参数影响数组大小时的默认数组大小 private static final Obj ...
- LinkedList源码阅读笔记(1.8)
目录 LinkedList类的注解阅读 LinkedList类的定义 属性的定义 LinkedList构造器 核心方法 校验方法 普通方法 迭代器(iterator&ListIterator) ...
- LinkedList源码阅读笔记
LinkedList LinkedList是双向链表,不循环(1.6之前循环),继承AbstractSequentialList类,实现了List, Deque, Cloneable接口. 链表的特点 ...
- LinkedList源码分析笔记(jdk1.8)
1.特点 LinkedList的底层实现是由一个双向链表实现的,可以从两端作为头节点遍历链表. 允许元素为null 线程不安全 增删相对ArrayList快,改查相对ArrayList慢(curd都会 ...
- JDK1.8源码阅读笔记(2) AtomicInteger AtomicLong AtomicBoolean原子类
JDK1.8源码阅读笔记(2) AtomicInteger AtomicLong AtomicBoolean原子类 Unsafe Java中无法直接操作一块内存区域,不能像C++中那样可以自己申请内存 ...
- JDK1.8源码阅读笔记(1)Object类
JDK1.8源码阅读笔记(1)Object类 Object 类属于 java.lang 包,此包下的所有类在使⽤时⽆需⼿动导⼊,系统会在程序编译期间⾃动 导⼊.Object 类是所有类的基类,当⼀ ...
- gogs 源码阅读笔记 001
gogs 源码阅读笔记 001 gogs项目相当不错,本笔记实际是基于gogs fork版本 git-122a66f. gitea (gitea版本由来)[https://blog.gitea.io/ ...
- faster rcnn源码阅读笔记1
自己保存的源码阅读笔记哈 faster rcnn 的主要识别过程(粗略) (开始填坑了): 一张3通道,1600*1600图像输入中,经过特征提取网络,得到100*100*512的feature ma ...
- HashMap源码阅读笔记
HashMap源码阅读笔记 本文在此博客的内容上进行了部分修改,旨在加深笔者对HashMap的理解,暂不讨论红黑树相关逻辑 概述 HashMap作为经常使用到的类,大多时候都是只知道大概原理,比如 ...
随机推荐
- 聊一聊PV和并发
最近和几个朋友,聊到并发和服务器的压力问题.很多朋友,不知道该怎么去计算并发?部署多少台服务器才合适? 所以,今天就来聊一聊PV和并发,还有计算web服务器的数量 的等方法.这些都是自己的想法加上一些 ...
- Bubble Cup 8 finals I. Robots protection (575I)
题意: 有一个正方形区域, 要求支持两个操作: 1.放置三角形,给定放置方向(有4个方向,直角边与坐标轴平行),直角顶点坐标,边长 2.查询一个点被覆盖了多少次 1<=正方形区域边长n<= ...
- Mac下安装ionic和cordova,并生成iOS项目
为了开发HTML5,除了最新使用React Native等之外,目前首选的为稳定的ionic+Angularjs来开发iOS和android. Ionic(ionicframework一款接近原生的H ...
- juery学习6——焦点事件
参考资料 深入理解javascript中的焦点管理:http://www.cnblogs.com/xiaohuochai/p/5874447.html
- Java优先队列
按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...
- 一款强大的Android网络渗透软件dsploit
dSploit是一款基于Android系统的功能十分全面强大的网络渗透工具,可以提供给网络安全工作人员检查网络的安全性.小黑这次主要使用了其中的"简易嗅探""会话劫持&q ...
- web前端知识体系总结
1. 前言 大约在几个月之前,让我看完了<webkit技术内幕>这本书的时候,突然有了一个想法.想把整个web前端开发所需要的知识都之中在一个视图中,形成一个完整的web前端知识体系,目的 ...
- RecyclerView各种报错
昨天有人提到RecyclerView,于是我就照着官方的文档研究了下使用方法,结果发现示例代码有问题真是醉. 自己修改后编译是没有问题的但是运行的时候总是报错,大意就是提示找不到RecyclerVie ...
- 中文编程语言Z语言开源正式开源!!!
(Z语言基于.NET环境,源码中有很多高技术的代码,让更多的人知道对大家有会有很好的帮助,请管理员一点要批准放在首页) 本人实现的中文编程语言Z语言现在正式开源,采用LGPL协议. 编译器核心的网址为 ...
- BZOJ2134——单选错位
1.题意:这就是说考试的时候抄串了一位能对几个(雾) 2.分析:这是一个期望问题,期望就是平均,E(a+b)=E(a)+E(b),所以我们直接算出每个点能对几个就好,那么就是1/max(a[i],a[ ...