当前位置:网站首页>ArrayList与LinkedList效率的对比
ArrayList与LinkedList效率的对比
2022-07-02 09:43:00 【牧歌ing】
ArrayList与LinkedList效率的对比
看了网上很多文章都说,ArrayList的查询效率优于LinkedList,而增删效率低于LinkedList。这种说法其实是有问题的,ArrayList底层是数组,插入其实就是在尾部添加,确实在扩容的时候会损失一点效率,但这点损失基本不需要考虑。
下面将用百万数据进行一个测试
首先看增加
long s=System.currentTimeMillis();
ArrayList arrayList=new ArrayList();
for (int i = 0; i < 1000000; i++) {
arrayList.add(i);
}
System.out.println("ArrayList尾插 "+(System.currentTimeMillis()-s));
s=System.currentTimeMillis();
LinkedList linkedList=new LinkedList();
for (int i = 0; i < 1000000; i++) {
linkedList.add(i);
}
System.out.println("LinkedList增加 "+(System.currentTimeMillis()-s));
结果是ArrayList的速度要优于LinkedList
删除
long s=System.currentTimeMillis();
String str="";
for (int i = 999999; i > -1; i--) {
arrayList.remove(i);
}
System.out.println("ArrayList尾删 "+(System.currentTimeMillis()-s));
s=System.currentTimeMillis();
for (int i = 999999; i > -1; i--) {
linkedList.removeLast();
}
System.out.println("linkedList尾删 "+(System.currentTimeMillis()-s));
结果是ArrayList的速度优于LinkedList
多执行几遍也是ArrayList的增删速度优于LinkedList



边栏推荐
- Bom Dom
- 线性DP AcWing 897. 最长公共子序列
- String palindrome hash template question o (1) judge whether the string is palindrome
- 哈希表 AcWing 841. 字符串哈希
- 分布式机器学习框架与高维实时推荐系统
- LeetCode—剑指 Offer 51. 数组中的逆序对
- Drools terminates the execution of other rules after executing one rule
- 堆 AcWing 839. 模拟堆
- Addition, deletion, modification and query of MySQL table (Advanced)
- Fastdateformat why thread safe
猜你喜欢

MySQL and PostgreSQL methods to grab slow SQL
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

哈希表 AcWing 840. 模拟散列表

CDH6之Sqoop添加数据库驱动

Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students

bellman-ford AcWing 853. 有边数限制的最短路

Tas (file d'attente prioritaire)

High performance erasure code coding

Go learning notes - multithreading

Writing method of then part in drools
随机推荐
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
线性DP AcWing 896. 最长上升子序列 II
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
[I'm a mound pytorch tutorial] learning notes
LeetCode—剑指 Offer 59 - I、59 - II
染色法判定二分图 AcWing 860. 染色法判定二分图
In development, why do you find someone who is paid more than you but doesn't write any code?
Is the neural network (pinn) with embedded physical knowledge a pit?
arcgis js 4.x 地图中加入图片
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
堆(優先級隊列)
Shuttle encapsulated AppBar
Brush questions --- binary tree --2
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Intel 内部指令 --- AVX和AVX2学习笔记
计算二叉树的最大路径和
High performance erasure code coding
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Distributed machine learning framework and high-dimensional real-time recommendation system
IPhone 6 plus is listed in Apple's "retro products" list