当前位置:网站首页>ArrayList 和 LinkedList 区别
ArrayList 和 LinkedList 区别
2022-08-09 22:01:00 【欣21】
ArrayList:基于数组实现;
LinkedList:基于双向链表实现;使用 Node 存储链表节点信息;
ArrayList 和 LinkedList 都实现了 List 接口,且两者都是线程不安全的
区别:
ArrayList 使用动态数组存储元素,而 LinkedList 使用双向链表来存储元素。
对于随机访问,ArrayList要优于LinkedList; 对于插入和删除操作,LinkedList优于ArrayList。
LinkedList比ArrayList更占内存,因为LinkedList的节点除了存储数据,还存储了两个引用,一个指向前一个元素,一个指向后一个元素。
ArrayList 只能用作列表;LinkedList 可以用作列表或者队列,因为它还实现了 Deque 接口。
在查询时,ArrayList 直接根据下标获取,和数组的长度无关,查询速度快;而LinkedList 则需要从头节点开始遍历,下标小于链表长度的一半时,从前往后遍历;否则从后往前遍历,查询速度慢。
在增加时,ArrayList 将新的元素添加的指定的位置时,会将插入位置后的元素依次向后复制,添加速度慢;而 LinkedList 则是可以直接加入进去,先用 n 记录 p.next ,然后 p.next = newNode ,再把 newNode.next = n;这样就实现了添加,而且速度更快。
在删除时,ArrayList 会将指定位置后的元素依次向前复制,删除速度也慢;而 LinkedList 则是将原来的 p.next 变为了p.next.next,就实现了删除,而且速度更快。
边栏推荐
猜你喜欢
Js fifteen interview questions (with answers)
shell学习
力扣 1413. 逐步求和得到正数的最小值
Metasploit常用命令、技术功能模块
Kubernetes Service对象
Jinshanyun earthquake, the epicenter is in bytes?
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
typedef和#define的花里胡哨的用法
任务流执行器是如何工作的?
国内手机厂商曾为它大打出手,如今它却最先垮台……
随机推荐
Analyze the Add() method in Fragment management from the source code
一文让你快速了解隐式类型转换【整型提升】!
你真的了解乐观锁和悲观锁吗?
leetcode:286.墙和门
华为鸿蒙3.0的野望:技术、应用、生态
Basic operations of openGauss database (super detailed)
Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
级联下拉菜单的实现「建议收藏」
Multiple reasons for MySQL slow query
R语言将列表数据转化为向量数据(使用unlist函数将列表数据转化为向量数据)
NodeJS使用JWT
十步以内,用小程序快速生成App!
【EF】数据表全部字段更新与部分字段更新
js数组对象去重
leetcode brush questions diary Calculate the number of elements on the right that is less than the current element
MySQL——JDBC
你的 Link Button 能让用户选择新页面打开吗?
Activiti7审批流
BulkInsert方法实现批量导入