When to use LinkedList and When to use ArrayList- An answer in StackOverFlow

本文深入探讨了Java中ArrayList与LinkedList的区别,包括它们的存储方式、操作效率和适用场景,帮助开发者根据具体需求选择合适的集合类。

arraylist-vs-linkedlist

https://i-blog.csdnimg.cn/blog_migrate/c616819f19b51ba13682c12729715232.png


LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array.

As with standard linked list and array operations, the various methods will have different algorithmic runtimes.

For LinkedList<E>

  • get(int index) is O(n)
  • add(E element) is O(1)
  • add(int index, E element) is O(n)
  • remove(int index) is O(n)
  • Iterator.remove() is O(1) <--- main benefit of LinkedList<E>
  • ListIterator.add(E element) is O(1) <--- main benefit of LinkedList<E>

For ArrayList<E>

  • get(int index) is O(1) <--- main benefit of ArrayList<E>
  • add(E element) is O(1) amortized, but O(n) worst-case since the array must be resized and copied
  • add(int index, E element) is O(n - index) amortized, but O(n) worst-case (as above)
  • remove(int index) is O(n - index) (i.e. removing last is O(1))
  • Iterator.remove() is O(n - index)
  • ListIterator.add(E element) is O(n - index)

LinkedList<E> allows for constant-time insertions or removals using iterators, but only sequential access of elements. In other words, you can walk the list forwards or backwards, but finding a position in the list takes time proportional to the size of the list.

ArrayList<E>, on the other hand, allow fast random read access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap. Also, if you add more elements than the capacity of the underlying array, a new array (1.5 times the size) is allocated, and the old array is copied to the new one, so adding to an ArrayList is O(n) in the worst case but constant on average.

So depending on the operations you intend to do, you should choose the implementations accordingly. Iterating over either kind of List is practically equally cheap. (Iterating over an ArrayList is technically faster, but unless you're doing something really performance-sensitive, you shouldn't worry about this -- they're both constants.)

The main benefits of using a LinkedList arise when you re-use existing iterators to insert and remove elements. These operations can then be done in O(1) by changing the list locally only. In an array list, the remainder of the array needs to be moved (i.e. copied). On the other side, seeking in a LinkedList means following the links in O(n), whereas in an ArrayList the desired position can be computed mathematically and accessed in O(1).

Also, if you have large lists, keep in mind that memory usage is also different. Each element of a LinkedList has more overhead since pointers to the next and previous elements are also stored. ArrayLists don't have this overhead. However, ArrayLists take up as much memory as is allocated for the capacity, regardless of whether elements have actually been added.

The default initial capacity of an ArrayList is pretty small (10 from Java 1.4 - 1.7). But since the underlying implementation is an array, the array must be resized if you add a lot of elements. To avoid the high cost of resizing when you know you're going to add a lot of elements, construct the ArrayList with a higher initial capacity.

It's worth noting that Vector also implements the List interface and is almost identical to ArrayList. The difference is that Vector is synchronized, so it is thread-safe. Because of this, it is also slightly slower than ArrayList. So as far as I understand, most Java programmers avoid Vector in favor of ArrayList since they will probably synchronize explicitly anyway if they care about that.

打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 QT框架是由Qt公司设计的一种跨平台C++图形用户界面应用程序开发工具包,该框架被广泛地应用于桌面电脑、移动设备以及嵌入式系统等领域。QTableView作为QT框架中的一个核心组件,其主要功能是用于展示表格形式的数据,并且常常与QAbstractItemModel或QSqlTableModel等模型类协同工作。在QTableView中嵌入自定义组件,例如按钮,能够实现更加多样化的用户交互功能。 在QT框架环境下,若想在QTableView的一列中嵌入两个按钮,我们需要掌握以下几个关键的技术要点: 1. **QTableView**:QTableView是QTableView类的一个实例,它提供了一个二维的表格视图界面,可以用来展示和编辑模型中的数据。QTableView能够显示由QAbstractItemModel子类所提供的数据,例如QStandardItemModel或QAbstractTableModel等。 2. **QTableWidgetItem**:在QTableView中,QTableWidgetItem是构成表格单元格的基本对象,它用于表示表格中每一行每一列的数据。在默认情况下,QTableView仅能展示文本信息,但通过继承QTableWidgetItem并重新绘制,我们可以实现自定义的内容,比如嵌入按钮。 3. **自定义视图项**:若要在单元格内部嵌入两个按钮,我们需要开发一个自定义的QTableWidgetItem子类,该子类中包含两个QPushButton。这个子类需要重写paintEvent()方法以绘制按钮,并且实现必要的信号和槽机制来处理按...
内容概要:本文系统研究了LLC谐振变换器的变频移相混合控制模型,并基于Simulink平台进行了完整的仿真实现。文章首先阐述了LLC谐振变换器在高频高效电源转换中的工作原理与技术优势,重点提出了一种融合变频控制与移相控制的混合调控策略,旨在拓宽输出调节范围并提升系统的动态响应能力与运行效率。通过建立精确的系统数学模型,设计了复合控制框图,并在Simulink中搭建仿真系统,全面验证了该控制策略在不同负载条件和输入电压波动下的稳定性、效率表现及软开关实现能力。仿真结果表明,所提出的混合控制方法能有效降低开关损耗,提高能量转换效率,具备良好的工程应用前景。; 适合人群:具备电力电子技术、自动控制理论基础,熟悉Simulink仿真环境,从事高频电源变换器、谐振变换器设计与优化的研究生、科研人员及电力电子领域工程技术人员。; 使用场景及目标:①用于高性能LLC谐振变换器控制系统的设计与动态性能优化;②为软开关技术在电力电子变换器中的应用提供仿真验证平台;③支撑相关课题的科研论文撰写、项目开发与创新方案验证。; 阅读建议:建议读者结合Simulink仿真模型文件进行同步操作,深入理解变频与移相控制的协调机制、控制环路设计及关键参数整定方法,重点关注软开关实现条件与系统效率优化路径,以促进理论研究向实际工程应用的转化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值