Vector与ArrayList实现大致相同,只是Vector再操作数据的实话会加锁,所以是线程安全的,其次Vector扩容大小默认为当前大小的额2倍,且其扩容值是可以指定的。部分源码如下:
public class Vector<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
//存放数据的数组
protected Object[] elementData;
//数据量 实际存放数据个数
protected int elementCount;
//每次扩容的大小
protected int capacityIncrement;
private static final long serialVersionUID = -2767605614048989439L;
/**
* 指定数组大小和每次扩容大小的构造函数
* @param initialCapacity 数组大小
* @param capacityIncrement 扩容参数
*/
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(int initialCapacity) {
this(initialCapacity, 0);
}
/** 空参构造函数,将数组大小设置为10 */
public Vector() {
this(10);
}
/**
* 根据集合数据初始化一个Vector
*/
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// 若数据不是Object[] 类型 则转换成Object[]类型
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
/**
* 将Vector中的数据复制到一个数组中
*/
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
/** 将数组大小设置为Vector大小 */
public synchronized void trimToSize() {
modCount++;
int oldCapacity = elementData.length; //获取大小
if (elementCount < oldCapacity) {
elementData = Arrays.copyOf(elementData, elementCount); //复制数据形成一个新的数组返回
}
}
/**
* 检查数组大小 若不足 则进行扩容
*/
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
/**
* 检查数组大小 若不足 则进行扩容
*/
private void ensureCapacityHelper(int minCapacity) {
if (minCapacity - elementData.length > 0)
grow(minCapacity); //需要扩容
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/** 扩容操作*/
private void grow(int minCapacity) {
int oldCapacity = elementData.length; //原大小
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity); //若设置了扩容值,数组大小+扩容值 否则扩容为当前大小的2倍
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity; //扩容之后还不够,直接设置为当前值
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity); //校验最大值
elementData = Arrays.copyOf(elementData, newCapacity);
}
/** 较大的值处理 */
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // 说明int最高位是1 太大了
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ? //设置最大值
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
* 设置大小
*/
public synchronized void setSize(int newSize) {
modCount++;
if (newSize > elementCount) {
ensureCapacityHelper(newSize); //数组大小不足,则进行扩容
} else {
for (int i = newSize ; i < elementCount ; i++) { //截取前newSize数据,之后的数据置空
elementData[i] = null;
}
}
elementCount = newSize; //重新设置Vector大小
}
/** 返回数组大小 */
public synchronized int capacity() {
return elementData.length;
}
/** 返回Vector大小 */
public synchronized int size() {
return elementCount;
}
/** 是否有数据 */
public synchronized boolean isEmpty() {
return elementCount == 0;
}
/** 获取数据枚举,类似于迭代器 用来遍历数据 */
public Enumeration<E> elements() {
return new Enumeration<E>() {
int count = 0; //当前数据下标
/** 是否有下一个数据 */
public boolean hasMoreElements() {
return count < elementCount;
}
/** 获取下一个数据 */
public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData(count++);
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}
/** 是否包含数据 */
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
/** 查找数据 从前往后进行查找 */
public int indexOf(Object o) {
return indexOf(o, 0);
}
/** 查找数据 从index位置往后找 */
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;
}
/** 查找数据 从后往前找 */
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
/** 查找数据 从index位置往前找 */
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
if (o == null) {
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/** 获取指定下标位置数据 */
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData(index);
}
/** 获取第一个数据 */
public synchronized E firstElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(0);
}
/** 获取最后一个数据 */
public synchronized E lastElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(elementCount - 1);
}
/** 修改指定位置的数据 */
public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
/** 删除指定位置数据 */
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) { //校验index
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) { //校验index
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1; //计算待移动的数据数
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j); //数据前移
}
elementCount--; //Vrctor数据大小-1
elementData[elementCount] = null; //清除最后一个节点数据
}
/** 指定位置插入数据 */
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++; //数据量+1
}
/** 添加数据到最后 */
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1); //需要扩容?
elementData[elementCount++] = obj; //添加数据
}
/** 删除数据 返回是否成功删除 */
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
/** 删除所有数据 清空 */
public synchronized void removeAllElements() {
modCount++;
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
/** clone */
public synchronized Object clone() {
try {
@SuppressWarnings("unchecked")
Vector<E> v = (Vector<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, elementCount);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/** 转换结果为数组 Object[]*/
public synchronized Object[] toArray() {
return Arrays.copyOf(elementData, elementCount);
}
/** 转换结果为数组T[]*/
@SuppressWarnings("unchecked")
public synchronized <T> T[] toArray(T[] a) {
if (a.length < elementCount)
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
System.arraycopy(elementData, 0, a, 0, elementCount);
if (a.length > elementCount)
a[elementCount] = null;
return a;
}
/** 获取指定下标节点 */
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
/** 获取指定下标数据 */
public synchronized E get(int index) {
if (index >= elementCount) //校验index大小
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
/** 修改数据 */
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
/** 添加数据 */
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
/** 删除数据 */
public boolean remove(Object o) {
return removeElement(o);
}
/** 指定位置插入数据 */
public void add(int index, E element) {
insertElementAt(element, index);
}
/** 删除指定位置数据 */
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 void clear() {
removeAllElements();
}
public synchronized boolean containsAll(Collection<?> c) {
return super.containsAll(c);
}
public synchronized boolean addAll(Collection<? extends E> c) {
modCount++;
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);
System.arraycopy(a, 0, elementData, elementCount, numNew);
elementCount += numNew;
return numNew != 0;
}
public synchronized boolean removeAll(Collection<?> c) {
return super.removeAll(c);
}
public synchronized boolean retainAll(Collection<?> c) {
return super.retainAll(c);
}
public synchronized boolean addAll(int index, Collection<? extends E> c) {
modCount++;
if (index < 0 || index > elementCount)
throw new ArrayIndexOutOfBoundsException(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);
int numMoved = elementCount - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
elementCount += numNew;
return numNew != 0;
}
public synchronized boolean equals(Object o) {
return super.equals(o);
}
public synchronized int hashCode() {
return super.hashCode();
}
public synchronized String toString() {
return super.toString();
}
}