《Java源码分析》:Vector_wojiushimogui的博客-CSDN博客
vector属性
//vector中的属性,用来存储数据的数组
protected Object[] elementData;
//用来记录已经存储数据的个数
protected int elementCount;
//扩容的大小,如果capacityIncrement<=0,默认扩为两倍。capacityIncrement>0就在原数组长度上加
//capacityIncrement
protected int capacityIncrement;
Vector构造函数
/*
其它的构造函数都是调用此构造函数
参数的说明
initialCapacity:指定的数组初始容量,默认值为10
capacityIncrement:指定的每次的扩容大小,默认值是0,
capacityIncrement=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(int initialCapacity) {
this(initialCapacity, 0);//默认扩容两倍
}public Vector() {//空参构造器
this(10);
}
add(E e)方法
添加元素的方法实现比较简单:直接在数组的后一个位置添加即可,不过在添加元素之前需要检查数组中是否已满,如果已满,则扩容。
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);//检查需不需要扩容
elementData[elementCount++] = e;
return true;
}
ensureCapacityHelper方法
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity); //当数组已满,则调用grow函数来对数组进行扩容
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;//最大值private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//扩容的大小由capacityIncrement决定,如果capacityIncrement<=0,则扩容到目前数组的两倍
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);//扩容
//检查是否newCapacity溢出了//如果扩容后的数组长度仍小于所需要的最小长度,就把新数组长度换为最小长度
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) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
add(int index, E element)
函数的功能:在指定位置添加元素
方法实现的思想也比较简单:首先检查是否需要扩容,如果需要,则进行扩容;接着将数组从索引index开始的元素全部向后移动一位。最后将元素保存在数组的index位置即可。
//在指定位置添加元素
public void add(int index, E element) {
insertElementAt(element, index);
}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++;
}
get(int index)方法
容器关键的两个方法分别为:添加元素、取出元素。因此,不可避免的我们需要分析下get方法的实现思路。
get方法就更简单了,由于Vector是借助于数组来实现的,因此get方法的内部实现也就是取出位置为index的元素。
public synchronized E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); return elementData(index); }
上面的get、add方法都是用了synchronized关键字进行修饰,说明他们是线程安全的,在多线程并发时,不需要我们进行额外的同步。而ArrayList是没有进行同步的。这也是Vector和ArrayList的一个区别
set(int index, E element)方法
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
remove(Object o)方法
函数的功能:去除掉元素o。
思路:先找到该元素在数组中的位置index;然后移除索引位置为index的元素即可
/*
函数的功能:去除掉元素o。
思路:先找到该元素在数组中的位置index;然后移除索引位置为index的元素即可
*/
public boolean remove(Object o) {
return removeElement(o);
}public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);//返回第一次出现的索引
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
public int indexOf(Object o) {
return indexOf(o, 0);
}
/*
函数的功能:找到元素o在数组在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 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 */
}
elements方法
方法中创建了一个枚举类型,里面实现还是通过元素是否存在,通过下标遍历数组,这里就过多的进行介绍。
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");
}
};
}