Skip to content

Commit 690c1b5

Browse files
committed
vector
1 parent 946347c commit 690c1b5

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

MD/ArrayList.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# ArrayList 的底层分析
1+
# ArrayList/Vector 的底层分析
2+
3+
## ArrayList
24

35
`ArrayList` 实现于 `List``RandomAccess` 接口。可以插入空数据并且允许数据为空,也支持随机随机访问。
46

@@ -52,3 +54,20 @@
5254
也是一个数组复制的过程。
5355

5456
由此可见 `ArrayList` 的主要消耗是数组扩容以及在指定位置添加数据,在日常使用时最好是指定大小,尽量减少扩容。更要减少在指定位置插入数据的操作。
57+
58+
59+
## Vector
60+
61+
`Voctor` 也是实现于 `List` 接口,底层数据结构和 `ArrayList` 类似,也是一个动态数组存放数据。不过是在 `add()` 方法的时候使用 `synchronize` 进行同步写数据,但是开销较大,所以 Vector 是一个同步容器并不是一个并发容器。
62+
63+
以下是 `add()` 方法:
64+
```java
65+
public synchronized boolean add(E e) {
66+
modCount++;
67+
ensureCapacityHelper(elementCount + 1);
68+
elementData[elementCount++] = e;
69+
return true;
70+
}
71+
```
72+
73+

0 commit comments

Comments
 (0)