用java源代码学数据结构<二>: Vector 详解(三)
dd(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);
}
//将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的顺序添加这些元素。
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);
}
//在指定位置将指定 Collection 中的所有元素插入到此向量中。
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();
}
public synchronized List subList(int fromIndex, int toIndex) {
return Collections.synchronizedList(super.subList(fromIndex, toIndex),this);
}
//从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。
protected synchronized void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = elementCount - toIndex;
//现将后面的对象移到前面来
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newElementCount = elementCount - (toIndex-fromIndex);
while (elementCount != newElementCount)
elementData[--elementCount] = null;
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
final java.io.ObjectOutputStream.PutField fields = s.putFields();
final Object[] data;
synchronized (this) {
fields.put("capacityIncrement", capacityIncrement);
fields.put("elementCount", elementCount);
data = elementData.clone();
}
fields.put("elementData", data);
s.writeFields();
}
public synchronized ListIterator listIterator(int index) {
if (index < 0 || index > elementCount)
throw new IndexOutOfBoundsException("Index: "+index);
retur