Vector详解
1.Vector是什么?
老样子,先来看一下UML类图:
再来看看源码:
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{...}
看起来和ArrayList很相似,Vector继承了AbstractList同时也实现了List接口,所以自然也就有了那些add,remove等等的方法但是它自己实现了一套自己的逻辑包装了一下,同时也实现了RandomAccess接口所以支持快速访问,实现了Cloneable接口也就支持了克隆,实现了Serializable接口也就支持了序列化,Vector虽说也是一个数组,但是它还有点特殊,算是一个矢量队列,是一个向量类,Vector也是基于一个容量能够动态增长的数组来实现的,该类是JDK1.0版本添加的类,它的很多实现方法都加了synchronized关键字,因此是线程安全的,虽然说它本身是线程安全的,但是多线程问题一直都是一个头疼的问题,所以在使用过程中还是要多考虑,安全的是它本身,而并不是它所处于的业务逻辑.
1.Vector怎么实现的?
老规矩继续往下说:
//重要参数
//矢量元素数组
protected Object[] elementData;
//有效矢量元素个数
protected int elementCount;
//当矢量的大小大于其容量时,其容量自动增加的量。如果容量增量小于或等于零,则向量的容量将在每次需要增长时翻倍。
protected int capacityIncrement;
//构造方法
public Vector() {
this(10);
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
上边可以看出来,容量默认为10,使用第一种方法系统会自动对向量进行管理,若使用后两种方法。则系统将根据参数,initialcapacity设定向量对象的容量(即向量对象可存储数据的大小),当真正存放的数据个数超过容量时。系统会扩充向量对象存储容量。参数capacityincrement给定了每次扩充的扩充值。当capacityincrement为0的时候,则每次扩充一倍,利用这个功能可以优化存储。
创建完了自然也就是增删改查了,其实ArrayList和它很相似,扩容机制相差无几,那咱们就慢慢分析一下:
//增
//在此向量的指定位置插入指定的元素,将当前位于该位置的元素(如果有)和任何后续元素右移(在其索引中添加一个元素)
public void add(int index, E element) {
insertElementAt(element, index);
}
//在指定的索引处将指定的对象作为此矢量中的元素插入;索引大于或等于指定index的向量中的每个元素都向上移动,使其索引大于以前的值,索引必须是大于或等于0且小于或等于矢量当前大小的值,(如果索引等于矢量的当前大小,则新元素将附加到矢量中)注意:add方法会颠倒参数的顺序,以便更接近于数组的使用。
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
//将指定的元素添加到此向量的末尾,将其大小增加一。如果这个向量的大小大于它的容量,它就会扩容。
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
//将指定的元素同步追加到此向量的末尾
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
//扩容
grow(minCapacity);
}
private void grow(int minCapacity) {
// 获取elementData的原始容量
int oldCapacity = elementData.length;
// 计算新的容量,如果在构造方法中设置了capacityIncrement > 0,那么新数组长度就是原数组长度 + capacityIncrement,否则,新数组长度就是原数组长度 * 2
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
// 若进行扩容后,capacity仍然比实际需要的小,则新容量更改为实际需要的大小,即minCapacity
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
// 如果新数组的长度比虚拟机能够提供给数组的最大存储空间大,则将新数组长度更改为最大正数:Integer.MAX_VALUE
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 按照新的容量newCapacity创建一个新数组,然后再将原数组中的内容copy到新数组中
elementData = Arrays.copyOf(elementData, newCapacity);
}
//删
//删除最先被索引到的元素,如果该向量不包含该元素,则该元素不变.
public boolean remove(Object o) {
return removeElement(o);
}
//删除此向量中第一次出现的指定元素。如果该向量不包含该元素,则该元素不变。也就是说,如果存在这样的元素就删除索引i最低的元素.
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
//删除此向量中指定位置的元素。将所有后续元素向左移动(从元素中减去一个元素),返回从向量中移除的元素。如果索引超出范围,则引发arrayIndexOutOfBoundsException
public synchronized E remove(int index) {
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
int numMoved = elementCount - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
//从该向量中移除所有元素,并将其大小设置为零。
public synchronized void removeAllElements() {
modCount++;
// Let gc do its work
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
//删除指定索引处的元素。此向量中索引大于或等于指定index的每个元素向下移动,使其索引小于以前的值。这个向量的大小减少1。(索引必须是大于或等于0且小于矢量当前大小的值。注意:remove方法返回存储在指定位置的旧值。)
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}
//改
//用指定的元素替换此向量中指定位置的元素,如果索引超出范围,则引发arrayIndexOutOfBoundsException
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
//查
//Vector查找元素时,是分为元素为null和不为null两种方式来判断的,这也说明Vector允许添加null元素;同时,如果这个元素在Vector中存在多个,则只会找出从index开始,最先出现的那个。其他的查找方法或者包含原理都是这些,就是通过循环遍历找到与之相等的元素并返回,如果没有那就返回0,默认返回-1.
public synchronized int indexOf(Object o, int index) {
if (o == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
这里既然说到了它的遍历,我之前做过一个实验,用4种方式去遍历Vector,分别是迭代器遍历,普通for循环,增强for循环和Enumeration遍历(while),最后结果是普通for效率最高,迭代器最慢.所以建议遍历Vector的时候还是采用随机访问的方式比较好!
1.Vector的优缺点
优点:
它是线程安全的,底层数据结构是数组(矢量队列),随机访问速度快.
缺点:
增加和删除效率比较慢.
提示:
目前很多都用ArrayList代替了Vector,对我来说我用这个集合类还是相对较少的,不是特殊情况下一般都用并发包下的类替代了,或者用Collections中的方法替换了.