当前位置:网站首页>[HBZ share] reasons for slow addition and deletion of ArrayList and fast query
[HBZ share] reasons for slow addition and deletion of ArrayList and fast query
2022-07-06 04:21:00 【hbz-】
ArrayList When is the query fast ?
- Only by subscript index When inquiring , Very fast performance , Because you can directly find the corresponding data in the following table , Time complexity O(1)
- When non subscript query , But through for Loop to traverse , The performance is not fast , Time complexity O(n)
- myth : Many people mistakenly think ArrayList Any inquiry is fast , It's actually wrong , Only through index Our inquiry is very fast
ArrayList Why the increase of performance is slow
- In image ArrayList Add elements to add When , Could lead to List Expansion of , because ArrayList The bottom layer is array structure , But the data does not support dynamic expansion , therefore ArrayList The capacity expansion mechanism of is to create a new array , Migrate the array data to the new array , Then add new elements
- The expansion mechanism is , If the original array does not exist , And expand the capacity directly 10 individual . If the original array exists , Then expand 1.5 By , namely oldSize + oldSize >> 1
ArrayList Why is the deletion performance slow
- Deletes the specified element ,ArrayList The bottom layer is actually deleted from index The position is the starting point , All elements to the last , Move one bit forward , Then set the last bit to null
- Move all elements , In fact, it is through the bottom System.arraycopy(), The array of index+1 Position start , Copied to the index Location , Then the last one =null
- So the process will be slow
ArrayList Modification of
- Modification is the same as query , When specifying index when , Directly modifying , A high performance O(1)
- When the cycle for To modify , Then the performance is low O(n)
边栏推荐
- cdc 能全量拉去oracle 表嘛
- 我想问一下 按照现在mysql-cdc的设计,全量阶段,如果某一个chunk的binlog回填阶段,
- 脚本生命周期
- How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
- flink sql 能同时读多个topic吗。with里怎么写
- 2/12 didn't learn anything
- Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
- CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
- In depth MySQL transactions, stored procedures and triggers
- Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
How does computer nail adjust sound
One question per day (Mathematics)
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
How to realize automatic playback of H5 video
食品行业仓储条码管理系统解决方案
JVM garbage collector concept
Query the number and size of records in each table in MySQL database
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
随机推荐
npm命令--安装依赖包--用法/详解
How can programmers resist the "three poisons" of "greed, anger and ignorance"?
Basic knowledge of binary tree, BFC, DFS
2/12 didn't learn anything
2328. Number of incremental paths in the grid graph (memory search)
软考 系统架构设计师 简明教程 | 总目录
P2022 interesting numbers (binary & digit DP)
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
The value of two date types is subtracted and converted to seconds
HotSpot VM
颠覆你的认知?get和post请求的本质
2327. Number of people who know secrets (recursive)
食品行业仓储条码管理系统解决方案
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
How to realize automatic playback of H5 video
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
Lambda expression learning
About some basic DP -- those things about coins (the basic introduction of DP)
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill