当前位置:网站首页>【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)
边栏推荐
- [leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
- Redis (replicate dictionary server) cache
- P2022 有趣的数(二分&数位dp)
- Ipv4中的A 、B、C类网络及子网掩码
- asp. Core is compatible with both JWT authentication and cookies authentication
- MySQL master-slave replication
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
- 2327. 知道秘密的人数(递推)
- math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
- 图应用详解
猜你喜欢

Database, relational database and NoSQL non relational database

DM8 backup set deletion

Thread sleep, thread sleep application scenarios

1291_ Add timestamp function in xshell log

MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】

Slow SQL fetching and analysis of MySQL database

How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server

R note prophet

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

math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
随机推荐
Stable Huawei micro certification, stable Huawei cloud database service practice
E. Best Pair
MySql數據庫root賬戶無法遠程登陸解决辦法
JVM garbage collector concept
BOM - location, history, pop-up box, timing
About some basic DP -- those things about coins (the basic introduction of DP)
Introduction to hashtable
Data processing methods - smote series and adasyn
深入浅出node模板解析错误escape is not a function
[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
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
Slow SQL fetching and analysis of MySQL database
Leetcode32 longest valid bracket (dynamic programming difficult problem)
Path of class file generated by idea compiling JSP page
51nod 1130 n factorial length V2 (Stirling approximation)
10个 Istio 流量管理 最常用的例子,你知道几个?
P3500 [POI2010]TES-Intelligence Test(二分&离线)
2/13 review Backpack + monotonic queue variant
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
[tomato assistant installation]