LinkedList
LinkedList和ArrayList都是List,不同的是ArrayList是以数组的形式来存储的,而LinkedList是以链表的形式存储的,ArrayList很简单,没什么可说的,下面就简单看一下LinkedList,他里面有个节点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;
- }
- }
并且LinkedList中有两个指针,一个指向头first,一个指向尾last,
- /**
- * Pointer to first node.
- * Invariant: (first == null && last == null) ||
- * (first.prev == null && first.item != null)
- */
- transient Node<E> first;
- /**
- * Pointer to last node.
- * Invariant: (first == null && last == null) ||
- * (last.next == null && last.item != null)
- */
- transient Node<E> last;
先来看第一个方法linkFirst
- /**
- * Links e as first element.
- */
- private void linkFirst(E e) {//在第一个节点前面添加一个节点
- final Node<E> f = first;//first节点指向头,把头结点保存在f中
- //创建新节点,新节点的前一个为null,因为新建的节点是要添加链表的头,所以他的前一个是为null,
- //新节点的下一个是f,这是因为新节点是要添加到f的前面的。
- final Node<E> newNode = new Node<>(null, e, f);
- first = newNode;//让新节点等于头结点
- //如果原来头结点为空,说明原来的链表为null,在创建第一个节点的时候first和last要指向同一个节点。
- if (f == null)
- last = newNode;
- else//如果原来链表不是null,让新节点等于原来头结点的前一个,
- f.prev = newNode;
- size++;//链表数量加1
- modCount++;//这个加是对链表操作的时候加,在Iterator遍历的时候是禁止操作的,否则要抛异常
- }
既然有添加到第一个,肯定会有添加到最后一个
- /**
- * Links e as last element.
- */
- void linkLast(E e) {
- //添加到最后
- final Node<E> l = last;
- //创建新节点,新节点的前一个是last,后一个为null,因为新节点就是最后一个,所以后一个要为null
- final Node<E> newNode = new Node<>(l, e, null);
- last = newNode;//让新建节点成为最后一个
- if (l == null)//如果之前的链表是null的,则first和last都要指向同一个节点
- first = newNode;
- else
- l.next = newNode;//让当前节点成为之前链表last的下一个
- size++;//加1
- modCount++;
- }
再看下一个方法linkBefore(E e, Node<E> succ)
- /**
- * Inserts element e before non-null Node succ.
- */
- void linkBefore(E e, Node<E> succ) {
- //他表示插入一个新节点e到succ节点的前面
- // assert succ != null;
- //首先succ节点要存在,否则要抛空指针异常,然后获取他的前一个节点
- final Node<E> pred = succ.prev;
- //创建新节点,新节点是前一个是pred,也就是succ的前一个,后一个是succ
- final Node<E> newNode = new Node<>(pred, e, succ);
- succ.prev = newNode;//新节点等于succ的前一个
- //pred等于null,说明succ是first节点,既然添加到succ的前面,所以就让新节点成为first节点
- if (pred == null)
- first = newNode;
- else
- pred.next = newNode;//让pred的下一个节点指向新节点
- size++;//加1
- modCount++;
- }
下一个方法是删除first节点
- /**
- * Unlinks non-null first node f.
- */
- private E unlinkFirst(Node<E> f) {
- //删除first节点,f就是first节点
- // 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;//让f的下一个节点成为first节点,
- //如果next为null,说明之前就一个first节点,删除之后就没有节点,所以只好让last节点指向null
- if (next == null)
- last = null;
- else
- next.prev = null;
- size--;
- modCount++;
- return element;
- }
然后下一个方法unlinkLast也都很简单,基本上没什么可说的,看下面一个方法unlink(Node<E> x)
- /**
- * Unlinks non-null node x.
- */
- E unlink(Node<E> x) {
- // assert x != null;
- //其实删除x节点很简单,就是让x的前一个后后一个连接就行了,不过还要考虑前一个和后一个是否为空的问题
- final E element = x.item;
- final Node<E> next = x.next;//x的下一个节点
- final Node<E> prev = x.prev;// x的前一个节点
- if (prev == null) {
- //前一个节点为空,说明x是first节点,所以要让next等于first
- first = next;
- } else {//连接
- prev.next = next;
- x.prev = null; // help GC
- }
- if (next == null) {//如果next为空,说明x是last节点,
- last = prev;
- } else {//连接
- next.prev = prev;
- x.next = null; // help GC
- }
- x.item = null;
- size--;
- modCount++;
- return element;
- }
下面再看一个方法remove(Object o)
- public boolean remove(Object o) {
- if (o == null) {//o为null的情况
- for (Node<E> x = first; x != null; x = x.next) {//对链表遍历
- if (x.item == null) {//找到之后删除
- unlink(x);
- return true;
- }
- }
- } else {//o不为空,和上面的差不多
- for (Node<E> x = first; x != null; x = x.next) {
- if (o.equals(x.item)) {
- unlink(x);
- return true;
- }
- }
- }
- return false;
- }
下面再看另一个方法node(int index),根据index查找node
- /**
- * Returns the (non-null) Node at the specified element index.
- */
- Node<E> node(int index) {
- // assert isElementIndex(index);
- 根据index查找节点,如果index在前半部分从前找,如果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;
- }
- }
其他的方法也都很简单,基本上没啥可说的,下面看最后一个方法addAll(int index, Collection<? extends E> c)
- /**
- * Inserts all of the elements in the specified collection into this
- * list, starting at the specified position. Shifts the element
- * currently at that position (if any) and any subsequent elements to
- * the right (increases their indices). The new elements will appear
- * in the list in the order that they are returned by the
- * specified collection's iterator.
- *
- * @param index index at which to insert the first element
- * from the specified collection
- * @param c collection containing elements to be added to this list
- * @return {@code true} if this list changed as a result of the call
- * @throws IndexOutOfBoundsException {@inheritDoc}
- * @throws NullPointerException if the specified collection is null
- */
- //把集合c添加到链表中,从index开始添加
- public boolean addAll(int index, Collection<? extends E> c) {
- checkPositionIndex(index);//检查index,如果越界会抛越界异常
- Object[] a = c.toArray();
- int numNew = a.length;
- if (numNew == 0)//如果c为空,则返回
- return false;
- //succ表示index位置的节点,pred表示succ的前一个节点
- Node<E> pred, succ;
- if (index == size) {//添加到最后
- succ = null;//index位置的节点为null,也就是succ为null
- pred = last;//last是前一个节点,因为要添加造last节点的后面
- } else {
- succ = node(index);//index位置的节点
- pred = succ.prev;//添加到pred和succ之间,所以要保存succ的前一个节点
- }
- for (Object o : a) {
- @SuppressWarnings("unchecked") E e = (E) o;
- //创建节点,前一个是pred,后一个是null,后一个在下面在赋值
- Node<E> newNode = new Node<>(pred, e, null);
- //前一个为null,说明原来链表是null的,让当前节点赋值first
- if (pred == null)
- first = newNode;
- else
- //先连接前面的,后面的先不连,newNode的前一个节点在创建的时候就已经赋值,而他前一个的下一个节点在这地方赋值,
- pred.next = newNode;
- pred = newNode;//让当前节点成为前一个,然后不停的往后添加
- }
- //到目前为止,前面的节点都已经连接完了,后面的都还没连接
- if (succ == null) {
- //这个简单,如果添加到原节点的最后,直接让pred等于last就行的,因为pred是最后一个添加的节点
- last = pred;
- } else {
- //如果不是添加到最后,需要最后添加的节点pred和succ连接起来即可。succ是pred的下一个节点,pred是succ的前一个节点
- pred.next = succ;
- succ.prev = pred;
- }
- size += numNew;//增加size
- modCount++;
- return true;
- }
LinkedList相关推荐
- 什么是LinkedList?什么时候使用它呢?Java LinkedList结构、用法及源码解析
前言:我们学习java时都知道ArrayList实现List接口,LinkedList也实现List接口,但我们平时用的时候LinkedList却很少被用到.那么,LinkedList什么时候该用到呢 ...
- 比较ArrayList、LinkedList、Vector
翻译人员: 铁锚 翻译时间: 2013年12月2日 原文链接: ArrayList vs. LinkedList vs. Vector 1. List概述 List,就如图名字所示一样,是元素的有序列 ...
- java arraylist和list_Java中ArrayList和LinkedList区别
原文链接:http://pengcqu.iteye.com/blog/502676 一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据 ...
- java链表list_java集合之linkedList链表基础
LinkedList链表: List接口的链接列表实现.允许存储所有元素(包含null).使用频繁增删元素. linkedList方法: void addFirst(E e) 指定元素插入列表的开头 ...
- 【java】兴唐第二十一节(LinkedList和泛型)
LinkedList知识点 1.实现了Iterable接口的类具有迭代功能. 2.List接口为Collection的子类,表示线形数据列表,其实现类有:ArrayList(数组线性表)与Linked ...
- arraylist 后往前遍历_面试官:谈谈常用的Arraylist和Linkedlist的区别
Arraylist:底层是基于动态数组,根据下表随机访问数组元素的效率高,向数组尾部添加元素的效率高:但是,删除数组中的数据以及向数组中间添加数据效率低,因为需要移动数组. 例如最坏的情况是删除第一个 ...
- Java编程的逻辑 (39) - 剖析LinkedList
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- ArrayList与LinkedList区别
1.ArrayList实现了基于动态数组的数据结构,LinkedList是实现了基于链表的数据结构. 2.对于随机访问get/set,ArrayList优于LinkedList,因为LinkedLis ...
- 某团技术拷问:ArrayList 和 LinkedList 哪个更占空间?
HR力荐了一个工作 4 年,目前年薪 40W+ 的候选人. 看他简历,从 JVM.MySQL.Redis,再到悲观锁.乐观锁一个都不缺,并发编程.分布式也都接触过,像是个实力派! 着急用人,就赶紧叫人 ...
- 从面试角度分析LinkedList源码
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 注:本系列文章中用到的jdk版本均为java8 Linke ...
最新文章
- 写的函数符号表里没有_你有没有想过,C语言 main 函数到底为啥这么写?
- 学习笔记:CentOS 7学习之十一:文件的重定向
- git add 命令的一个习惯用法:逐个挑选改动
- javascript 校验 非空_Javascript的表单与验证-非空验证
- 888. 公平的糖果棒交换
- dell服务器r730老自动重启_Dell R730服务器安装windows server 2008 R2蓝屏问题
- 快速浏览Silverlight3 beta:鸡肋一样的WritableBitmap
- js基础知识汇总03
- 微端服务器物品备注,HeroM2引擎怎么在装备上添加备注 传奇添加物品备注说明的方法...
- pycharm的python解释器选择_pycharm中配置python解释器
- Java实现飞机大战(详细思路与过程,含源代码)
- 昂达 v891 连接上adb 调试
- Yii中处理前后台登录新方法 | 饭饭博客
- linux之mail命令发邮件
- C盘ProgramData变得巨大--VS2010在C盘下生成的.iTrace文件解决办法
- 统计二叉树中不平衡节点树的个数
- 尤雨溪:Vue Function-based API RFC
- Java工作流管理系统(activity6.0)
- MySQL(九):InnoDB 表空间(Tables)
- mama计算机乐谱,lil mama钢琴简谱 Jain演唱 李兰妈妈
热门文章
- 【清华集训2017模拟】Catalan
- 分享一套开源的即时通讯 IM 聊天系统(附源码)
- LilyPond教程(0)——目录和索引
- 企业邮箱如何申请?如何用手机号注册邮箱?
- 机器学习吴恩达第二周
- python内置库求复数的辐角_根据下列选项,回答 30~34 题: A.杜仲B.黄柏C.厚朴D.肉桂E.牡丹皮 第 30 题 断面较平坦,粉...
- 数据库三大范式详解,部分依赖、完全依赖、传递依赖
- 搭建个人博客 步骤详述(hexo +github)
- 【优化调度】基于matlab遗传算法求解公交车调度排班优化问题【含Matlab源码 2212期】
- 为什么Google Home将成为Amazon Echo最可怕的噩梦?