标签:
1,Vector:protected Object[] elementData;
protected int elementCount;
protected int 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() {
this(10);
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public synchronized void addElement(E obj) {
modCount++;//结构化修改参数加1
ensureCapacityHelper(elementCount + 1);//确保capacity大小满足增加元素要求
elementData[elementCount++] = obj;//存储元素,数组元素索引加1
}
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;//获取数组长度
if (minCapacity > oldCapacity) {//如果当前存放元素索引大于数组长度
Object[] oldData = elementData;
int newCapacity = (capacityIncrement > 0) ?
(oldCapacity + capacityIncrement) : (oldCapacity * 2);//每次数组长度增加默认正常长度,默认增长长度为0时,扩充数组长度为原来的两倍
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = Arrays.copyOf(elementData, newCapacity);//数组复制
}
}
public synchronized boolean removeElement(Object obj) {
modCount++;//结构化修改计数加1
int i = indexOf(obj);//获取元素位置
if (i >= 0) {
removeElementAt(i);//移除对应位置的元素
return true;
}
return false;
}
public int indexOf(Object o) {
return indexOf(o, 0);
}
public synchronized int indexOf(Object o, int index) {
if (o == null) {//如果元素为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]))//元素不为null,调用equals方法来判断是否相等
return i;
}
return -1;
}
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
public synchronized int capacity() {
return elementData.length;
}
public synchronized int size() {
return elementCount;
}
标签:
原文地址:http://blog.csdn.net/mergades/article/details/43051305