您的位置:首页 > 编程语言 > Java开发

源码学习之Vector

2016-05-30 15:51 363 查看

Vector源码分析学习

同样,首先是Vector的定义

//继承AbstractList抽线类,实现了List、RandomAccess、Cloneable和Serializable接口
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable


Vector源码开头定义的几个成员变量

//这个就是实际存储数据的数组,从这个可以看出Vector是数组实现的
protected Object[] elementData;
//Vector内有效元素的数量
protected int elementCount;
//Vector容量增量,当capacityIncrement>0时,需要扩容时,增加的的是capacityIncrement,否则增加的容量是oldCapacity
protected int capacityIncrement;


构造函数,Vector一共提供了三个构造函数。

//构造函数,initialCapacity初始化容量,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;
}
//构造函数,initialCapacity初始化容量,capacityIncrement设置为0
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
//构造函数,initialCapacity设置为10,capacityIncrement设置为0
public Vector() {
this(10);
}
//构造函数,从另外一个集合初始化Vector
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);
}


Vector扩容机制

//保证容量
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
//保证容量,如果minCapacity大于Vector的容量,那么需要扩容
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

private void grow(int minCapacity) {
// overflow-conscious code
//如果capacityIncrement大于0的时候,扩容capacityIncrement,否则扩容oldCapacity
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
//如果新容量不能满足最小容量要求,那么新容量设置成minCapacity
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//如果新容量大于MAX_ARRAY_SIZE,那么新容量设置成Integer.MAX_VALUE
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;
}


Vector常用操作

//返回Vector的size()
public synchronized int size() {
return elementCount;
}
//判断Vector是否是空
public synchronized boolean isEmpty() {
return elementCount == 0;
}
//判断是否包含o元素
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
//返回第一个出现o的下标
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;
}
//获取index下标的元素值
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);

return elementData(index);
}
//设置index,下标的元素值为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;
}
//增加一个元素e
public synchronized boolean add(E e) {
modCount++;
//检查是否需要扩容
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
//移除vector中等于o的元素
public boolean remove(Object o) {
return removeElement(o);
}
//在下标index处增加了一个element
public void add(int index, E element) {
insertElementAt(element, index);
}
//移除下标为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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  源码 java Vector