Cheat Sheet: Speed of Common Operations
|
add to end |
remove |
insert at middle |
remove |
Random Access |
In-order |
Search for |
Notes
| |
|
Array |
O(n) |
O(n) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
Most efficient use of memory; use in cases where data size is fixed. |
|
List<T> |
best case O(1); worst case O(n) |
O(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
Implementation is optimized |
|
Collection<T> |
best case O(1); worst case O(n) |
O(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
List is a better choice, unless |
|
LinkedList<T> |
O(1) |
O(1) |
O(1) |
O(1) |
O(n) |
O(1) |
O(n) |
Many operations are fast, |
|
Stack<T> |
best case O(1); worst case O(n) |
O(1) |
N/A |
N/A |
N/A |
N/A |
N/A |
Shouldn't be selected for |
|
Queue<T> |
best case O(1); worst case O(n) |
O(1) |
N/A |
N/A |
N/A |
N/A |
N/A |
Shouldn't be selected for |
|
Dictionary<K,T> |
best case O(1); worst case O(n) |
O(1) |
best case O(1); worst case O(n) |
O(1) |
O(1)* |
O(1)* |
O(1) |
Although in-order access time |
本文对比了数组、List、Collection、LinkedList、Stack、Queue及Dictionary等数据结构在执行常见操作如添加、删除、随机访问等时的时间复杂度。通过这些信息可以帮助开发者选择最适合其应用场景的数据结构。
1800

被折叠的 条评论
为什么被折叠?



