当前位置:网站首页>【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)
边栏推荐
- 2327. 知道秘密的人数(递推)
- TCP/IP协议里面的网关地址和ip地址有什么区别?
- 《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
- [face recognition series] | realize automatic makeup
- Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
- ESP32_ FreeRTOS_ Arduino_ 1_ Create task
- . Net interprocess communication
- MySQL master-slave replication
- HotSpot VM
- ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
猜你喜欢
[face recognition series] | realize automatic makeup
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
Introduction to hashtable
R note prophet
Class A, B, C networks and subnet masks in IPv4
JVM garbage collector concept
自动化测试的好处
Mysql数据库慢sql抓取与分析
How does technology have the ability to solve problems perfectly
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
随机推荐
Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
[Zhao Yuqiang] deploy kubernetes cluster with binary package
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
How to execute an SQL statement in MySQL
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
Overturn your cognition? The nature of get and post requests
Introduction to hashtable
Solve the compilation problem of "c2001: line breaks in constants"
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
Recommendation system (IX) PNN model (product based neural networks)
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
Chinese brand hybrid technology: there is no best technical route, only better products
Record the pit of NETCORE's memory surge
Python book learning notes - Chapter 09 section 01 create and use classes
HotSpot VM
E. Best Pair
One question per day (Mathematics)
729. 我的日程安排表 I(set or 动态开点线段树)
QML和QWidget混合开发(初探)