JDK版本为1.8.0_271,Vector底层也是使用object数组,但使用synchronized 关键字来确保线程安全,但效率比ArrayList低,比较少见。这里以插入删除元素为例:
//属性
protected Object[] elementData;
protected int elementCount;//构造器
public Vector() {this(10); //指定初始容量initialCapacity为10
}public Vector(int initialCapacity) {this(initialCapacity, 0); //指定capacityIncrement增量为0
}public Vector(int initialCapacity, int capacityIncrement) {super();//判断了形参初始容量initialCapacity的合法性if (initialCapacity < 0)throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);//创建了一个Object[]类型的数组this.elementData = new Object[initialCapacity];//增量,默认是0,如果是0,后面就按照2倍增加,如果不是0,后面就按照你指定的增量进行增量this.capacityIncrement = capacityIncrement;
}//方法:add()相关方法
//synchronized意味着线程安全的
public synchronized boolean add(E e) {modCount++;//看是否需要扩容ensureCapacityHelper(elementCount + 1);//把新的元素存入[elementCount],存入后,elementCount元素的个数增1elementData[elementCount++] = e;return true;
}// 如果需要扩容的话会进行扩容
private void ensureCapacityHelper(int minCapacity) {//看是否超过了当前数组的容量if (minCapacity - elementData.length > 0)grow(minCapacity); //扩容
}// 扩容
private void grow(int minCapacity) {// overflow-conscious codeint oldCapacity = elementData.length; //获取原有数组的长度//如果capacityIncrement增量是0,新容量 = oldCapacity的2倍//如果capacityIncrement增量是不是0,新容量 = oldCapacity + capacityIncrement增量;int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity);//如果按照上面计算的新容量还不够,就按照需要的最小容量来扩容minCapacityif (newCapacity - minCapacity < 0)newCapacity = minCapacity;//如果新容量超过了最大数组限制(Integer.MAX_VALUE - 8),那么单独处理if (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);//把旧数组中的数据复制到新数组中,新数组的长度为newCapacityelementData = Arrays.copyOf(elementData, newCapacity);
}// 新容量超过了最大数组限制(Integer.MAX_VALUE - 8;),需要进行调整
private static int hugeCapacity(int minCapacity) {if (minCapacity < 0) // overflowthrow new OutOfMemoryError();// 对minCapacity和MAX_ARRAY_SIZE进行比较// 若minCapacity大,将Integer.MAX_VALUE作为新数组的大小// 若MAX_ARRAY_SIZE大,将MAX_ARRAY_SIZE作为新数组的大小// MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;
}
------
著作权归JavaGuide(javaguide.cn)所有
基于MIT协议
原文链接:https://javaguide.cn/java/collection/arraylist-source-code.html//方法:remove()相关方法
public boolean remove(Object o) {return removeElement(o);
}
// 移除元素
public synchronized boolean removeElement(Object obj) {modCount++;//查找obj在当前Vector中的下标int i = indexOf(obj);//如果i>=0,说明存在,删除[i]位置的元素if (i >= 0) {removeElementAt(i);return true;}return false;
}//方法:indexOf(),查找元素
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)//如果是null值,用==null判断return i;} else {//要查找的元素是非null值,防止NullPointerExceptionfor (int i = index ; i < elementCount ; i++)if (o.equals(elementData[i]))//如果是非null值,用equals判断return i;}return -1;
}//方法:removeElementAt(),移除index位置的元素
public synchronized void removeElementAt(int index) {modCount++;//判断下标的合法性,必须在[0,elementCount)合法区间内if (index >= elementCount) {throw new ArrayIndexOutOfBoundsException(index + " >= " +elementCount);}else if (index < 0) {throw new ArrayIndexOutOfBoundsException(index);}//j是要移动的元素的个数int j = elementCount - index - 1;//如果需要移动元素,就调用System.arraycopy进行移动if (j > 0) {//把index+1位置以及后面的元素往前移动//index+1的位置的元素移动到index位置,依次类推//一共移动j个System.arraycopy(elementData, index + 1, elementData, index, j);}//元素的总个数减少elementCount--;//将elementData[elementCount]这个位置置空,用来添加新元素,位置的元素等着被GC回收elementData[elementCount] = null; /* to let gc do its work */
}