底层也是一个数组
初始化容量:10
怎么扩容?
扩容之后是原容量的 2 倍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16private Object[] grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = ArraysSupport.newLength(oldCapacity,
minCapacity - oldCapacity, /* minimum growth */
capacityIncrement > 0 ? capacityIncrement : oldCapacity /* preferred growth */);
return elementData = Arrays.copyOf(elementData, newCapacity);
}
/**
* The amount by which the capacity of the vector is automatically
* incremented when its size becomes greater than its capacity. If
* the capacity increment is less than or equal to zero, the capacity
* of the vector is doubled each time it needs to grow.
*
* @serial
*/
protected int capacityIncrement;
ArrayList 集合扩容是原容量的 1.5 倍
Vector 中所有的方法都是线程同步的,都带有
synchronized
关键字,是线程安全的,效率比较低,使用较少了怎么将一个线程不安全的 ArrayList 集合转换成线程安全的呢?
- 使用集合工具类:
java.util.Collections;
- 注意:
java.util.Collection
是集合接口java.util.Collections
是集合工具类
- 使用集合工具类:
1 | import java.util.ArrayList; |