当前位置:网站首页>【HBZ分享】ArrayList的增删慢查询快的原因
【HBZ分享】ArrayList的增删慢查询快的原因
2022-07-06 04:16:00 【hbz-】
ArrayList什么情况下的查询才很快?
- 只有通过下标index查询时,性能才很快,因为可以直接找到对应下表数据,时间复杂度O(1)
- 当非下标查询,而是通过for循环来遍历时,性能并不快,时间复杂度时O(n)
- 误区:很多人误以为ArrayList任何情况查询都很快,其实是错的,只有通过index的查询才很快
ArrayList的增加为什么性能慢
- 在像ArrayList中加入元素add的时候,有可能会导致List的扩容, 因为ArrayList底层是数组结构,但数据不支持动态扩容,所以ArrayList的扩容机制就是再创建一个新数组,把就数组数据迁移到新数组,然后再加入新元素
- 扩容机制是,如果原数组不存在,则直接扩容10个。如果原数组存在,则扩大1.5被,即oldSize + oldSize >> 1
ArrayList的删除为什么性能慢
- 删除指定元素,ArrayList底层实际是从删除的index位置为起点起点,到最后一个的所有元素,向前移动一位,然后把最后一位设置成null
- 移动所有元素,实际就是通过底层System.arraycopy(),将数组的index+1位置开始,复制到index位置,然后最后一位=null
- 所以这个过程会很慢
ArrayList的修改
- 修改和查询一样,当指定index时,直接修改,性能很高O(1)
- 当循环for去修改,那就性能低O(n)
边栏推荐
- math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
- asp. Core is compatible with both JWT authentication and cookies authentication
- 1008 circular right shift of array elements (20 points)
- Brief tutorial for soft exam system architecture designer | general catalog
- 绑定在游戏对象上的脚本的执行顺序
- Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
- About some basic DP -- those things about coins (the basic introduction of DP)
- Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
- Web components series (VII) -- life cycle of custom components
- 20、 EEPROM memory (AT24C02) (similar to AD)
猜你喜欢

Security xxE vulnerability recurrence (XXe Lab)
![[Zhao Yuqiang] deploy kubernetes cluster with binary package](/img/45/6777fa919386e526dbb0d2c808a7f2.jpg)
[Zhao Yuqiang] deploy kubernetes cluster with binary package

Figure application details

Yyds dry goods inventory web components series (VII) -- life cycle of custom components

Record the pit of NETCORE's memory surge

Mysql数据库慢sql抓取与分析

MySql数据库root账户无法远程登陆解决办法

The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched

食品行业仓储条码管理系统解决方案

JVM garbage collector concept
随机推荐
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Mysql数据库慢sql抓取与分析
【leetcode】1189. Maximum number of "balloons"
2/11 matrix fast power +dp+ bisection
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
POI add border
MySql數據庫root賬戶無法遠程登陸解决辦法
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
C. The Third Problem(找规律)
BOM - location, history, pop-up box, timing
Stable Huawei micro certification, stable Huawei cloud database service practice
Use js to complete an LRU cache
Redis (replicate dictionary server) cache
P2102 地砖铺设(dfs&贪心)
Overturn your cognition? The nature of get and post requests
Codeforces Round #770 (Div. 2) B. Fortune Telling
View 工作流程
R note prophet
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】