JavaSE——集合4:LinkedList、ArrayList与LinkedList的选择

embedded/2024/10/19 2:23:55/

目录

LinkedList%E7%9A%84%E5%85%A8%E9%9D%A2%E8%AF%B4%E6%98%8E-toc" style="margin-left:0px;">一、LinkedList的全面说明

LinkedList%E7%9A%84%E5%BA%95%E5%B1%82%E6%93%8D%E4%BD%9C%E6%9C%BA%E5%88%B6-toc" style="margin-left:0px;">二、LinkedList的底层操作机制

LinkedList%E6%B7%BB%E5%8A%A0%E7%BB%93%E7%82%B9%E6%BA%90%E7%A0%81-toc" style="margin-left:40px;">(一)LinkedList添加结点源码

LinkedList%E5%88%A0%E9%99%A4%E7%BB%93%E7%82%B9%E6%BA%90%E7%A0%81%C2%A0-toc" style="margin-left:40px;">(二)LinkedList删除结点源码 

LinkedList%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95-toc" style="margin-left:0px;">三、LinkedList常用方法

ArrayList%E4%B8%8ELinkedList%E7%9A%84%E9%80%89%E6%8B%A9%C2%A0-toc" style="margin-left:0px;">四、ArrayListLinkedList的选择 


一、LinkedList的全面说明

  1. LinkedList底层实现了双向链表和双端队列的特点
  2. 可以添加任意元素(元素可以重复),包括null
  3. 线程不安全,没有实现同步和互斥

LinkedList%E7%9A%84%E5%BA%95%E5%B1%82%E6%93%8D%E4%BD%9C%E6%9C%BA%E5%88%B6">二、LinkedList的底层操作机制

  1. LinkedList底层维护了一个双向链表
  2. LinkedList中维护了两个属性first和last,分别指向首节点和尾节点
  3. 每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个节点。最终实现双向链表
  4. 所以LinkedList元素的添加和删除,不是通过数组完成的,相对来说效率较高

LinkedList%E6%B7%BB%E5%8A%A0%E7%BB%93%E7%82%B9%E6%BA%90%E7%A0%81">(一)LinkedList添加结点源码

1. LinkedList linkedList = new LinkedList();public LinkedList() {}
2. 这时 linkeList 的属性 first = null  last = null
3. 执行 添加public boolean add(E e) {linkLast(e);return true;}
4.将新的结点,加入到双向链表的最后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;elsel.next = newNode;size++;modCount++;}

LinkedList%E5%88%A0%E9%99%A4%E7%BB%93%E7%82%B9%E6%BA%90%E7%A0%81%C2%A0">(二)LinkedList删除结点源码 

linkedList.remove(); // 这里默认删除的是第一个结点1. 执行 removeFirstpublic E remove() {return removeFirst();}
2. 执行public E removeFirst() {final Node<E> f = first;if (f == null)throw new NoSuchElementException();return unlinkFirst(f);}
3. 执行 unlinkFirst, 将 f 指向的双向链表的第一个结点拿掉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 GCfirst = next;if (next == null)last = null;elsenext.prev = null;size--;modCount++;return element;}

LinkedList%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95">三、LinkedList常用方法

        因为LinkedList也继承了Collection和List,所以List的方法也适用于LinkedList

  1. add()
  2. remove()    // 默认删除第一个结点
  3. removeFirst()
  4. removeLast()
  5. set(索引值,插入的元素)
  6. get(索引值)
public static void main(String[] args) {LinkedList linkedList = new LinkedList();linkedList.add(1);linkedList.add(2);linkedList.add(3);linkedList.add("");linkedList.add(" ");linkedList.add(' ');linkedList.add(null);linkedList.add(null);System.out.println("linkedList=" + linkedList);// linkedList=[1, 2, 3, ,  ,  , null, null]// remove默认删除第一个结点linkedList.remove();System.out.println("linkedList=" + linkedList);// linkedList=[2, 3, ,  ,  , null, null]// 修改某个结点对象linkedList.set(1, 999);System.out.println("linkedList=" + linkedList);// linkedList=[2, 999, ,  ,  , null, null]// 得到某个结点对象// get(1) 是得到双向链表的第二个对象Object o = linkedList.get(1);System.out.println(o); // 999// 因为LinkedList 是 实现了List接口, 遍历方式System.out.println("===LinkeList遍历迭代器====");Iterator iterator = linkedList.iterator();while (iterator.hasNext()) {Object next = iterator.next();System.out.println("next=" + next);}// next=2// next=999// next=// next=// next=// next=null// next=nullSystem.out.println("===LinkeList遍历增强for====");for (Object o1 : linkedList) {System.out.println("o1=" + o1);}// next=2// next=999// next=// next=// next=// next=null// next=nullSystem.out.println("===LinkeList遍历普通for====");for (int i = 0; i < linkedList.size(); i++) {System.out.println(linkedList.get(i));}// 2// 999//////// null// null
}

ArrayList%E4%B8%8ELinkedList%E7%9A%84%E9%80%89%E6%8B%A9%C2%A0">四、ArrayListLinkedList的选择 

底层结构增删的效率改查的效率
ArrayList可变数组较低;底层依赖数组扩容较高;根据数组索引查找
LinkedList双向链表较高;底层通过链表追加较低;在链表中从头到尾遍历

如何选择ArrayListLinkedList

  1. 如果改查的操作多,选择ArrayList
  2. 如果增删的操作多,选择LinkedList
  3. 一般来说,80%-90%都是查询,因此大部分情况下会选择ArrayList
  4. 根据业务灵活选择,也可能一个模块使用的是ArrayList,另一个模块使用LinkedList

http://www.ppmy.cn/embedded/126950.html

相关文章

计组_输入输出系统

2024.08.05&#xff1a;计算机组成原理输入输出学习笔记 第25节 输入输出系统 5.1 IO基本职能5.2 IO接口的通用结构5.3 IO数据传送控制方式5.3.1 程序直接控制&#xff08;程序查询控制&#xff09;&#xff08;1&#xff09;独占查询&#xff08;2&#xff09;定时查询 5.3.2 …

Spring Boot知识管理系统:敏捷开发实践

3系统分析 3.1可行性分析 通过对本知识管理系统实行的目的初步调查和分析&#xff0c;提出可行性方案并对其一一进行论证。我们在这里主要从技术可行性、经济可行性、操作可行性等方面进行分析。 3.1.1技术可行性 本知识管理系统采用JAVA作为开发语言&#xff0c;Spring Boot框…

使用SIFT算法实现可缩放比例的图像匹配:Python与OpenCV

使用SIFT算法实现强大的图像匹配&#xff1a;Python与OpenCV实战指南 在计算机视觉领域,图像匹配是一个常见而重要的任务。无论是在图像拼接、对象识别还是视觉跟踪中,我们都需要可靠的方法来找出一个图像在另一个图像中的位置。今天,我们将探讨一种强大的技术 —— SIFT(尺度…

Android 中获取当前 CPU 频率和占用率

最近在优化 App 的性能&#xff0c;需要获取当前 CPU视频频率和占用率&#xff0c;通过查询资料&#xff0c;大致思路如下&#xff1a; 目前没有标准的 API 来获取 CPU 的使用频率&#xff0c;只能通过读取指定 CPU 文件获取当前 CPU 频率&#xff0c;在某些机器或者特定版本中…

如何通过 Nginx 只允许 www 域名访问并禁止裸域名访问

个人名片 &#x1f393;作者简介&#xff1a;java领域优质创作者 &#x1f310;个人主页&#xff1a;码农阿豪 &#x1f4de;工作室&#xff1a;新空间代码工作室&#xff08;提供各种软件服务&#xff09; &#x1f48c;个人邮箱&#xff1a;[2435024119qq.com] &#x1f4f1…

代码随想录打卡Day57

今天真的好累&#xff0c;开完组会刷今天的题刷了一天。还有助教的事情没做完&#xff0c;还有一些其他的破事全都对在一堆了。今天的前两道题自己AC的&#xff0c;后面两道题看题解写的&#xff0c;最后一道题的思路就算想出来了&#xff0c;实现起来也不简单。。。。处在红温…

Spring Boot 项目中 Redis 与数据库性能对比实战:从缓存配置到时间分析,详解最佳实践

一、前言&#xff1a; 在现代应用中&#xff0c;随着数据量的增大和访问频率的提高&#xff0c;如何提高数据存取的性能变得尤为重要。缓存技术作为一种常见的优化手段&#xff0c;被广泛应用于减少数据库访问压力、提升系统响应速度。Redis 作为一种高效的内存缓存数据库&…

索引面试题总结

索引就是一种有序的数据结构 索引是帮助存储引擎快速获取数据的一种数据结构。 索引结构 按数据结构分&#xff1a; Btree索引,Hash索引,Full-text索引&#xff0c;Full-text 我们平常所说的索引&#xff0c;如果没有特别指明&#xff0c;都是指 B 树结构组织的索引。 创建…