首先我们来看JDK源码中Java.util.Vector的代码,剔除所有的方法和静态变量,
Java.lang.Vector的核心代码如下:
- public class Vector<E>
- extends AbstractList<E>
- implements List<E>, RandomAccess, Cloneable, java.io.Serializable
- {
-
- 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(int initialCapacity) {
- this(initialCapacity, 0);
- }
-
-
- public Vector() {
- this(10);
- }
-
-
- public Vector(Collection<? extends E> c) {
- elementData = c.toArray();
- elementCount = elementData.length;
- if (elementData.getClass() != Object[].class)
- elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
- }
下面我们再看一下 Vector类的 add(E e)方法的源代码:
-
- public synchronized boolean add(E e) {
- modCount++;
- ensureCapacityHelper(elementCount + 1);
- elementData[elementCount++] = e;
- return true;
- }
-
- private void ensureCapacityHelper(int minCapacity) {
- int oldCapacity = elementData.length;
- if (minCapacity > oldCapacity) {
- Object[] oldData = elementData;
- int newCapacity = (capacityIncrement > 0) ?
- (oldCapacity + capacityIncrement) : (oldCapacity * 2);
-
- if (newCapacity < minCapacity) {
- newCapacity = minCapacity;
- }
- elementData = Arrays.copyOf(elementData, newCapacity);
- }
- }
下面我们再看一下 Vector类的 remove(Object o)方法的源代码:
- 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 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;
- }
从上面的代码我们可以看出 Vector每次只删除最靠前的那个相符的变量。
下面我们再看一下 Vector类的 equals(Object o)方法的源代码:
-
- public synchronized boolean equals(Object o) {
- return super.equals(o);
- }
-
-
-
- public boolean equals(Object o) {
- if (o == this)
- return true;
- if (!(o instanceof List))
- return false;
-
- ListIterator<E> e1 = listIterator();
- ListIterator e2 = ((List) o).listIterator();
- while(e1.hasNext() && e2.hasNext()) {
- E o1 = e1.next();
- Object o2 = e2.next();
- if (!(o1==null ? o2==null : o1.equals(o2)))
-
- return false;
- }
- return !(e1.hasNext() || e2.hasNext());
- }
下面我们再看一下 Vector类的 hashCode()方法的源代码:
-
- public synchronized int hashCode() {
- return super.hashCode();
- }
-
- public int hashCode() {
- int hashCode = 1;
- Iterator<E> i = iterator();
- while (i.hasNext()) {
- E obj = i.next();
- hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
-
- }
- return hashCode;
- }