2.LinkedList

2.1 UML继承关系图

2.2 底层存储节点

  • 通过内部类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;
}
}
  • add方法是在最后一个node节点增加关联

    可以看出LinkedList是开链表,首尾不相连,不会形成闭环
transient Node<E> last;
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++;
}
  • get,remove方法

    对list长度除以2,判断是前部分还是后部分,再进行,循环取值;
 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;
}
}

2.3 ListIterator迭代器

  • 可以指定开始迭代位置
 ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
  • remove,set方法前必须要用next()方法,将当前节点缓存到lastReturned
private Node<E> lastReturned;//用于存储切换的node节点
  • add 方法根据节点位置,进行判断在节点之前增加,还是节点之后增加
 lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);

2.3 常用增删方法实现

  • poll,poll,removeFirst 方法,从头部取出数据,并从list中移除该节点
 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;
}

-push 方法是从头部增加节点数据

2.4 LinkedList 内部提供了切分的方法

 LLSpliterator<E> implements Spliterator<E>

总结一下:

1.LinkedList的存储时通过Node对象存储的,Node对象有pre,next双向链关联前后Node;FirstNode的pre为空,LastNode的next为空,不能形成闭合的链表;

2.迭代器在移除Node之前,必须使用node方法,缓存当前节点信息否则会报错;

3.LinkedList提供了pop,pull等方法,从头部获取并移除Node

最新文章

  1. 那些PHP中没有全称的简写
  2. ZJOI day1总结
  3. poj 1995 裸快速幂
  4. users
  5. PAT (Basic Level) Practise:1008. 数组元素循环右移问题
  6. 10.26最后的模拟DAY2 数字对[暴力]
  7. akoj-1059-Picture
  8. Kubernetes v1.6开始支持RBAC
  9. 怎样把Linux的私钥文件id_rsa转换成putty的ppk格式
  10. 1. Nagios和 NagiosQL安装及配置
  11. 有趣的 zkw 线段树(超全详解)
  12. MacOS使用Charles抓去HTTPS数据
  13. 【C#复习总结】析构函数
  14. PythonStudy——文件操作 File operation
  15. 《剑指offer》-判断平衡二叉树
  16. iOS开源项目之日志框架CocoaLumberjack
  17. cocos2d-js Shader系列2:在cc.Sprite上使用Shader(黑白、灰度、造旧效果)
  18. Exception in thread main java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFacto
  19. 三网合一 中国移动铁通光猫 HG6821M 如何设置宽带自动连接
  20. LR-IE录制设置

热门文章

  1. java实现第三届蓝桥杯方块填数
  2. Java实现第十届蓝桥杯不同子串
  3. PAT 在霍格沃茨找零钱
  4. 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&amp;指定扫描规则
  5. 解Bug之路-记一次JVM堆外内存泄露Bug的查找
  6. vue对象数组数据变化,页面不渲染
  7. TD课程通的最终版评价
  8. WDCP3.3中多PHP版本安装方法,以及安装遇到的问题
  9. 【转载】有人出天价买他的一个文案标题,今天10min教会你……
  10. controller场景设计