最近在看JDK源码,从源码的角度记录一下ArrayList和Vector的一些区别
1、new
a、不指定长度
Vector默认创建10个元素的数组
public Vector() {
this(10);
}
ArrayList默认指向一个0个元素的数组
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
b、指定长度
Vector创建指定元素个数的数组,默认增长量为0
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
ArrayList创建指定元素个数的数组
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
c、指定长度,指定增量
Vector创建指定元素个数的数组,指定增长量
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
ArrayList无此种构造方法
d、elementData
Vector
/**
* The array buffer into which the components of the vector are
* stored. The capacity of the vector is the length of this array buffer,
* and is at least large enough to contain all the vector's elements.
*
* <p>Any array elements following the last element in the Vector are null.
*
* @serial
*/
protected Object[] elementData;
ArrayList
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData;
可以看到ArrayList的elementData有transient修饰,也就是说ArrayList的elementData不能被序列化;
同时ArrayList的elementData是private修饰,而Vector的elementData是protected修饰,
也就是说ArrayList的elementData只有ArrayList才能访问,而Vector的elementData同一个package和子类可以访问
2、增长
Vector默认增长当前数组元素个数的一倍,如果new对象时指定了initialIncrement,则增长量为initialIncrement
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
ArrayList默认增长当前数组元素个数的一半
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
Vector和ArrayList都是newCapacity<minCapacity则newCapacity=minCapacity,确保最小容量
3、线程安全
Vector是线程安全的,很多方法有synchronized
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
ArrayList是线程不安全的
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}