当前位置:网站首页>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,就实现了删除,而且速度更快。
边栏推荐
- shell学习
- A. Common Prefixes
- leetcode:332. 重新安排行程
- Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
- Flask introductory learning tutorial
- 你的 Link Button 能让用户选择新页面打开吗?
- xctf攻防世界 Web高手进阶区 ics-05
- JS解混淆-AST还原案例
- 1215 – Cannot add foreign key constraint
- Space not freed after TRUNCATE table
猜你喜欢

leetcode 39. 组合总和(完全背包问题)

【微服务~Nacos】Nacos服务提供者和服务消费者

xctf攻防世界 Web高手进阶区 ics-05

Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum

shell学习

Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut

阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
![[Microservice~Nacos] Configuration Center of Nacos](/img/c3/9d8fb0fd49a0ebab43ed604f9bd1cc.png)
[Microservice~Nacos] Configuration Center of Nacos

从产品角度看 L2 应用:为什么说这是一个游乐场?

月薪5K的运维小白如何成为月薪5W的高级架构师?
随机推荐
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
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!
C. Omkar and Baseball
力扣 1413. 逐步求和得到正数的最小值
2.1.5 大纲显示问题
为什么这么多人都想当产品经理?
18-GuliMall 压力测试与性能监控
leetcode:320.列举单词的全部缩写
Analyze the Add() method in Fragment management from the source code
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
关于ETL的两种架构(ETL架构和ELT架构)
[Microservice~Nacos] Configuration Center of Nacos
mysql 找不到或无法加载已注册的 .Net Framework Data Provider。
Basic operations of openGauss database (super detailed)
Kubernetes Service对象
navicat 快捷键
js数组对象去重
nvm下node安装;node环境变量配置
大型分布式存储方案MinIO介绍,看完你就懂了!
js十五道面试题(含答案)