当前位置:网站首页>【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)
边栏推荐
- What is the difference between gateway address and IP address in tcp/ip protocol?
- 颠覆你的认知?get和post请求的本质
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- IDEA编译JSP页面生成的class文件路径
- QML和QWidget混合开发(初探)
- Solve the compilation problem of "c2001: line breaks in constants"
- Fedora/REHL 安装 semanage
- 2327. 知道秘密的人数(递推)
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- In depth MySQL transactions, stored procedures and triggers
猜你喜欢
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
MySql数据库root账户无法远程登陆解决办法
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
Comprehensive ability evaluation system
Tips for using dm8huge table
Solve the compilation problem of "c2001: line breaks in constants"
Record an excel xxE vulnerability
R note prophet
Security xxE vulnerability recurrence (XXe Lab)
Proof of Stirling formula
随机推荐
P2102 地砖铺设(dfs&贪心)
2/11 matrix fast power +dp+ bisection
C. The Third Problem(找规律)
Lambda expression learning
Cross domain and jsonp details
P3500 [POI2010]TES-Intelligence Test(二分&离线)
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
电脑钉钉怎么调整声音
Redis (replicate dictionary server) cache
HotSpot VM
Maxay paper latex template description
Determine which week of the month the day is
Overturn your cognition? The nature of get and post requests
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
How to execute an SQL statement in MySQL
Python book learning notes - Chapter 09 section 01 create and use classes
Solve the compilation problem of "c2001: line breaks in constants"
Proof of Stirling formula
80% of the diseases are caused by bad living habits. There are eight common bad habits, which are both physical and mental
[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