Skip to content

Commit 08d705d

Browse files
committed
[U] list: vector
1 parent 48e383c commit 08d705d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Vector
2+
3+
## 数组容量自增
4+
5+
```java
6+
private void grow(int minCapacity) {
7+
// overflow-conscious code
8+
int oldCapacity = elementData.length;
9+
// 默认容量翻倍
10+
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
11+
capacityIncrement : oldCapacity);
12+
if (newCapacity - minCapacity < 0)
13+
newCapacity = minCapacity;
14+
if (newCapacity - MAX_ARRAY_SIZE > 0)
15+
newCapacity = hugeCapacity(minCapacity);
16+
elementData = Arrays.copyOf(elementData, newCapacity);
17+
}
18+
```
19+
20+
`ArrayList` 的区别是默认增长策略直接翻倍,且是同步的
21+
22+
## 最后
23+
24+
fast-fail。线程安全。
25+
26+
没啥好说的。

0 commit comments

Comments
 (0)