LinkedList和ArrayList都是List,不同的是ArrayList是以数组的形式来存储的,而LinkedList是以链表的形式存储的,ArrayList很简单,没什么可说的,下面就简单看一下LinkedList,他里面有个节点Node,是双向的链表

[java] view plaincopy
  1. private static class Node<E> {
  2. E item;
  3. Node<E> next;
  4. Node<E> prev;
  5. Node(Node<E> prev, E element, Node<E> next) {
  6. this.item = element;
  7. this.next = next;
  8. this.prev = prev;
  9. }
  10. }

并且LinkedList中有两个指针,一个指向头first,一个指向尾last,

[java] view plaincopy
  1. /**
  2. * Pointer to first node.
  3. * Invariant: (first == null && last == null) ||
  4. *            (first.prev == null && first.item != null)
  5. */
  6. transient Node<E> first;
  7. /**
  8. * Pointer to last node.
  9. * Invariant: (first == null && last == null) ||
  10. *            (last.next == null && last.item != null)
  11. */
  12. transient Node<E> last;

先来看第一个方法linkFirst

[java] view plaincopy
  1. /**
  2. * Links e as first element.
  3. */
  4. private void linkFirst(E e) {//在第一个节点前面添加一个节点
  5. final Node<E> f = first;//first节点指向头,把头结点保存在f中
  6. //创建新节点,新节点的前一个为null,因为新建的节点是要添加链表的头,所以他的前一个是为null,
  7. //新节点的下一个是f,这是因为新节点是要添加到f的前面的。
  8. final Node<E> newNode = new Node<>(null, e, f);
  9. first = newNode;//让新节点等于头结点
  10. //如果原来头结点为空,说明原来的链表为null,在创建第一个节点的时候first和last要指向同一个节点。
  11. if (f == null)
  12. last = newNode;
  13. else//如果原来链表不是null,让新节点等于原来头结点的前一个,
  14. f.prev = newNode;
  15. size++;//链表数量加1
  16. modCount++;//这个加是对链表操作的时候加,在Iterator遍历的时候是禁止操作的,否则要抛异常
  17. }

既然有添加到第一个,肯定会有添加到最后一个

[java] view plaincopy
  1. /**
  2. * Links e as last element.
  3. */
  4. void linkLast(E e) {
  5. //添加到最后
  6. final Node<E> l = last;
  7. //创建新节点,新节点的前一个是last,后一个为null,因为新节点就是最后一个,所以后一个要为null
  8. final Node<E> newNode = new Node<>(l, e, null);
  9. last = newNode;//让新建节点成为最后一个
  10. if (l == null)//如果之前的链表是null的,则first和last都要指向同一个节点
  11. first = newNode;
  12. else
  13. l.next = newNode;//让当前节点成为之前链表last的下一个
  14. size++;//加1
  15. modCount++;
  16. }

再看下一个方法linkBefore(E e, Node<E> succ)

[java] view plaincopy
  1. /**
  2. * Inserts element e before non-null Node succ.
  3. */
  4. void linkBefore(E e, Node<E> succ) {
  5. //他表示插入一个新节点e到succ节点的前面
  6. // assert succ != null;
  7. //首先succ节点要存在,否则要抛空指针异常,然后获取他的前一个节点
  8. final Node<E> pred = succ.prev;
  9. //创建新节点,新节点是前一个是pred,也就是succ的前一个,后一个是succ
  10. final Node<E> newNode = new Node<>(pred, e, succ);
  11. succ.prev = newNode;//新节点等于succ的前一个
  12. //pred等于null,说明succ是first节点,既然添加到succ的前面,所以就让新节点成为first节点
  13. if (pred == null)
  14. first = newNode;
  15. else
  16. pred.next = newNode;//让pred的下一个节点指向新节点
  17. size++;//加1
  18. modCount++;
  19. }

下一个方法是删除first节点

[java] view plaincopy
  1. /**
  2. * Unlinks non-null first node f.
  3. */
  4. private E unlinkFirst(Node<E> f) {
  5. //删除first节点,f就是first节点
  6. // assert f == first && f != null;
  7. final E element = f.item;
  8. final Node<E> next = f.next;
  9. f.item = null;
  10. f.next = null; // help GC
  11. first = next;//让f的下一个节点成为first节点,
  12. //如果next为null,说明之前就一个first节点,删除之后就没有节点,所以只好让last节点指向null
  13. if (next == null)
  14. last = null;
  15. else
  16. next.prev = null;
  17. size--;
  18. modCount++;
  19. return element;
  20. }

然后下一个方法unlinkLast也都很简单,基本上没什么可说的,看下面一个方法unlink(Node<E> x)

[java] view plaincopy
  1. /**
  2. * Unlinks non-null node x.
  3. */
  4. E unlink(Node<E> x) {
  5. // assert x != null;
  6. //其实删除x节点很简单,就是让x的前一个后后一个连接就行了,不过还要考虑前一个和后一个是否为空的问题
  7. final E element = x.item;
  8. final Node<E> next = x.next;//x的下一个节点
  9. final Node<E> prev = x.prev;// x的前一个节点
  10. if (prev == null) {
  11. //前一个节点为空,说明x是first节点,所以要让next等于first
  12. first = next;
  13. } else {//连接
  14. prev.next = next;
  15. x.prev = null;  // help GC
  16. }
  17. if (next == null) {//如果next为空,说明x是last节点,
  18. last = prev;
  19. } else {//连接
  20. next.prev = prev;
  21. x.next = null;  // help GC
  22. }
  23. x.item = null;
  24. size--;
  25. modCount++;
  26. return element;
  27. }

下面再看一个方法remove(Object o)

[java] view plaincopy
  1. public boolean remove(Object o) {
  2. if (o == null) {//o为null的情况
  3. for (Node<E> x = first; x != null; x = x.next) {//对链表遍历
  4. if (x.item == null) {//找到之后删除
  5. unlink(x);
  6. return true;
  7. }
  8. }
  9. } else {//o不为空,和上面的差不多
  10. for (Node<E> x = first; x != null; x = x.next) {
  11. if (o.equals(x.item)) {
  12. unlink(x);
  13. return true;
  14. }
  15. }
  16. }
  17. return false;
  18. }

下面再看另一个方法node(int index),根据index查找node

[java] view plaincopy
  1. /**
  2. * Returns the (non-null) Node at the specified element index.
  3. */
  4. Node<E> node(int index) {
  5. // assert isElementIndex(index);
  6. 根据index查找节点,如果index在前半部分从前找,如果index在后半部分,从后面查找
  7. if (index < (size >> 1)) {
  8. Node<E> x = first;
  9. for (int i = 0; i < index; i++)
  10. x = x.next;
  11. return x;
  12. } else {
  13. Node<E> x = last;
  14. for (int i = size - 1; i > index; i--)
  15. x = x.prev;
  16. return x;
  17. }
  18. }

其他的方法也都很简单,基本上没啥可说的,下面看最后一个方法addAll(int index, Collection<? extends E> c)

[java] view plaincopy
  1. /**
  2. * Inserts all of the elements in the specified collection into this
  3. * list, starting at the specified position.  Shifts the element
  4. * currently at that position (if any) and any subsequent elements to
  5. * the right (increases their indices).  The new elements will appear
  6. * in the list in the order that they are returned by the
  7. * specified collection's iterator.
  8. *
  9. * @param index index at which to insert the first element
  10. *              from the specified collection
  11. * @param c collection containing elements to be added to this list
  12. * @return {@code true} if this list changed as a result of the call
  13. * @throws IndexOutOfBoundsException {@inheritDoc}
  14. * @throws NullPointerException if the specified collection is null
  15. */
  16. //把集合c添加到链表中,从index开始添加
  17. public boolean addAll(int index, Collection<? extends E> c) {
  18. checkPositionIndex(index);//检查index,如果越界会抛越界异常
  19. Object[] a = c.toArray();
  20. int numNew = a.length;
  21. if (numNew == 0)//如果c为空,则返回
  22. return false;
  23. //succ表示index位置的节点,pred表示succ的前一个节点
  24. Node<E> pred, succ;
  25. if (index == size) {//添加到最后
  26. succ = null;//index位置的节点为null,也就是succ为null
  27. pred = last;//last是前一个节点,因为要添加造last节点的后面
  28. } else {
  29. succ = node(index);//index位置的节点
  30. pred = succ.prev;//添加到pred和succ之间,所以要保存succ的前一个节点
  31. }
  32. for (Object o : a) {
  33. @SuppressWarnings("unchecked") E e = (E) o;
  34. //创建节点,前一个是pred,后一个是null,后一个在下面在赋值
  35. Node<E> newNode = new Node<>(pred, e, null);
  36. //前一个为null,说明原来链表是null的,让当前节点赋值first
  37. if (pred == null)
  38. first = newNode;
  39. else
  40. //先连接前面的,后面的先不连,newNode的前一个节点在创建的时候就已经赋值,而他前一个的下一个节点在这地方赋值,
  41. pred.next = newNode;
  42. pred = newNode;//让当前节点成为前一个,然后不停的往后添加
  43. }
  44. //到目前为止,前面的节点都已经连接完了,后面的都还没连接
  45. if (succ == null) {
  46. //这个简单,如果添加到原节点的最后,直接让pred等于last就行的,因为pred是最后一个添加的节点
  47. last = pred;
  48. } else {
  49. //如果不是添加到最后,需要最后添加的节点pred和succ连接起来即可。succ是pred的下一个节点,pred是succ的前一个节点
  50. pred.next = succ;
  51. succ.prev = pred;
  52. }
  53. size += numNew;//增加size
  54. modCount++;
  55. return true;
  56. }

LinkedList相关推荐

  1. 什么是LinkedList?什么时候使用它呢?Java LinkedList结构、用法及源码解析

    前言:我们学习java时都知道ArrayList实现List接口,LinkedList也实现List接口,但我们平时用的时候LinkedList却很少被用到.那么,LinkedList什么时候该用到呢 ...

  2. 比较ArrayList、LinkedList、Vector

    翻译人员: 铁锚 翻译时间: 2013年12月2日 原文链接: ArrayList vs. LinkedList vs. Vector 1. List概述 List,就如图名字所示一样,是元素的有序列 ...

  3. java arraylist和list_Java中ArrayList和LinkedList区别

    原文链接:http://pengcqu.iteye.com/blog/502676 一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据 ...

  4. java链表list_java集合之linkedList链表基础

    LinkedList链表: List接口的链接列表实现.允许存储所有元素(包含null).使用频繁增删元素. linkedList方法: void addFirst(E e) 指定元素插入列表的开头 ...

  5. 【java】兴唐第二十一节(LinkedList和泛型)

    LinkedList知识点 1.实现了Iterable接口的类具有迭代功能. 2.List接口为Collection的子类,表示线形数据列表,其实现类有:ArrayList(数组线性表)与Linked ...

  6. arraylist 后往前遍历_面试官:谈谈常用的Arraylist和Linkedlist的区别

    Arraylist:底层是基于动态数组,根据下表随机访问数组元素的效率高,向数组尾部添加元素的效率高:但是,删除数组中的数据以及向数组中间添加数据效率低,因为需要移动数组. 例如最坏的情况是删除第一个 ...

  7. Java编程的逻辑 (39) - 剖析LinkedList

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  8. ArrayList与LinkedList区别

    1.ArrayList实现了基于动态数组的数据结构,LinkedList是实现了基于链表的数据结构. 2.对于随机访问get/set,ArrayList优于LinkedList,因为LinkedLis ...

  9. 某团技术拷问:ArrayList 和 LinkedList 哪个更占空间?

    HR力荐了一个工作 4 年,目前年薪 40W+ 的候选人. 看他简历,从 JVM.MySQL.Redis,再到悲观锁.乐观锁一个都不缺,并发编程.分布式也都接触过,像是个实力派! 着急用人,就赶紧叫人 ...

  10. 从面试角度分析LinkedList源码

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 注:本系列文章中用到的jdk版本均为java8 Linke ...

最新文章

  1. 写的函数符号表里没有_你有没有想过,C语言 main 函数到底为啥这么写?
  2. 学习笔记:CentOS 7学习之十一:文件的重定向
  3. git add 命令的一个习惯用法:逐个挑选改动
  4. javascript 校验 非空_Javascript的表单与验证-非空验证
  5. 888. 公平的糖果棒交换
  6. dell服务器r730老自动重启_Dell R730服务器安装windows server 2008 R2蓝屏问题
  7. 快速浏览Silverlight3 beta:鸡肋一样的WritableBitmap
  8. js基础知识汇总03
  9. 微端服务器物品备注,HeroM2引擎怎么在装备上添加备注 传奇添加物品备注说明的方法...
  10. pycharm的python解释器选择_pycharm中配置python解释器
  11. Java实现飞机大战(详细思路与过程,含源代码)
  12. 昂达 v891 连接上adb 调试
  13. Yii中处理前后台登录新方法 | 饭饭博客
  14. linux之mail命令发邮件
  15. C盘ProgramData变得巨大--VS2010在C盘下生成的.iTrace文件解决办法
  16. 统计二叉树中不平衡节点树的个数
  17. 尤雨溪:Vue Function-based API RFC
  18. Java工作流管理系统(activity6.0)
  19. MySQL(九):InnoDB 表空间(Tables)
  20. mama计算机乐谱,lil mama钢琴简谱 Jain演唱 李兰妈妈

热门文章

  1. 【清华集训2017模拟】Catalan
  2. 分享一套开源的即时通讯 IM 聊天系统(附源码)
  3. LilyPond教程(0)——目录和索引
  4. 企业邮箱如何申请?如何用手机号注册邮箱?
  5. 机器学习吴恩达第二周
  6. python内置库求复数的辐角_根据下列选项,回答 30~34 题: A.杜仲B.黄柏C.厚朴D.肉桂E.牡丹皮 第 30 题 断面较平坦,粉...
  7. 数据库三大范式详解,部分依赖、完全依赖、传递依赖
  8. 搭建个人博客 步骤详述(hexo +github)
  9. 【优化调度】基于matlab遗传算法求解公交车调度排班优化问题【含Matlab源码 2212期】
  10. 为什么Google Home将成为Amazon Echo最可怕的噩梦?