Vector
和ArrayList
之間的差別是一些這樣的:
Vector
而ArrayList
不同步是同步的。所以,Vector是線程安全的。
Vector
速度慢,因爲它是線程安全的。比較ArrayList
是非快速的,因爲它是非同步的。
A Vector
默認情況下其數組的大小增加了一倍。而當您將一個元素插入ArrayList
時,它會將陣列大小增加50%。
的ArrayList:
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); // 50%
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);
}
矢量:
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity); // default 100%
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
ArrayList中沒有定義的增量大小。向量定義增量大小。
/**
* 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
能不能調整就像Vector
。
ArrayList
不是線程安全的。它不能用多個線程直接替代Vector
由ArrayList
。
它大部分可以用單線程代替Vector
由ArrayList
。由於Vector
和ArrayList
聲明:
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
你期待什麼故障? – assylias
[Vector is broken](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated),你應該**使用'ArrayList',並且如果您需要同步,請使用'synchronizedList'。 – Idos
在現有代碼中,向量中添加了URL列表(沒有最大限制)。我害怕如果我使用具有50%增量的ArrayList可能會破壞該功能。 – MonsterJava