当前位置:网站首页>【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)
边栏推荐
- Global and Chinese market of aircraft anti icing and rain protection systems 2022-2028: Research Report on technology, participants, trends, market size and share
- The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
- Unity中几个重要类
- Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
- How to execute an SQL statement in MySQL
- 2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
- VPP性能测试
- 729. 我的日程安排表 I(set or 动态开点线段树)
- Slow SQL fetching and analysis of MySQL database
- Script lifecycle
猜你喜欢
10個 Istio 流量管理 最常用的例子,你知道幾個?
Solve the compilation problem of "c2001: line breaks in constants"
Lora gateway Ethernet transmission
Overturn your cognition? The nature of get and post requests
Mlapi series - 04 - network variables and network serialization [network synchronization]
[tomato assistant installation]
Thread sleep, thread sleep application scenarios
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
IDEA编译JSP页面生成的class文件路径
One question per day (Mathematics)
随机推荐
Benefits of automated testing
判断当天是当月的第几周
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
Cross domain and jsonp details
DM8 archive log file manual switching
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
图应用详解
Proof of Stirling formula
In depth MySQL transactions, stored procedures and triggers
Redis (replicate dictionary server) cache
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
食品行业仓储条码管理系统解决方案
E. Best Pair
10个 Istio 流量管理 最常用的例子,你知道几个?
[face recognition series] | realize automatic makeup
How to execute an SQL statement in MySQL
Basic use of MySQL (it is recommended to read and recite the content)
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Lora gateway Ethernet transmission
Solution to the problem that the root account of MySQL database cannot be logged in remotely