Vector简介
Vector也是基于数组实现的,是一个动态数组,其容量能自动增长。
LinkedList是JDK1.0引入了,它的许多实现要领都插手了同步语句,因此是线程安详的(其实也只是相对安详,有些时候照旧要插手同步语句来担保线程的安详),可以用于多线程情况。
LinkedList没有丝线Serializable接口,因此它不支持序列化,实现了Cloneable接口,能被克隆,实现了RandomAccess接口,支持快速随时机见。
Vector源码分解
Vector的源码如下(插手了较量具体的注释):
package java.util;
public class Vectorextends AbstractListimplements List, RandomAccess, Cloneable, java.io.Serializable
{
// 生存Vector中数据的数组
protected Object[] elementData;
// 实际数据的数量
protected int elementCount;
// 容量增长系数
protected int capacityIncrement;
// Vector的序列版本号
private static final long serialVersionUID = -2767605614048989439L;
// Vector结构函数。默认容量是10。
public Vector() {
this(10);
}
// 指定Vector容量巨细的结构函数
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
// 指定Vector"容量巨细"和"增长系数"的结构函数
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
// 新建一个数组,数组容量是initialCapacity
this.elementData = new Object[initialCapacity];
// 配置容量增长系数
this.capacityIncrement = capacityIncrement;
}
// 指定荟萃的Vector结构函数。
public Vector(Collection c) {
// 获取“荟萃(c)”的数组,并将其赋值给elementData
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的全部元素都拷贝到数组anArray中
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
// 将当前容量值设为 =实际元素个数
public synchronized void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (elementCount < oldCapacity) {
elementData = Arrays.copyOf(elementData, elementCount);
}
}
// 确认“Vector容量”的辅佐函数
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;
// 当Vector的容量不敷以容纳当前的全部元素,增加容量巨细。
// 若 容量增量系数>0(即capacityIncrement>0),则将容量增大当capacityIncrement
// 不然,将容量增大一倍。
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的容量。
public synchronized void ensureCapacity(int minCapacity) {
// 将Vector的改变统计数+1
modCount++;
ensureCapacityHelper(minCapacity);
}
// 配置容量值为 newSize
public synchronized void setSize(int newSize) {
modCount++;
if (newSize > elementCount) {
// 若 "newSize 大于 Vector容量",则调解Vector的巨细。
ensureCapacityHelper(newSize);
} else {
// 若 "newSize 小于/便是 Vector容量",则将newSize位置开始的元素都配置为null
for (int i = newSize ; i < elementCount ; i++) {
elementData[i] = null;
}
}
elementCount = newSize;
}
// 返回“Vector的总的容量”
public synchronized int capacity() {
return elementData.length;
}
// 返回“Vector的实际巨细”,即Vector中元素个数
public synchronized int size() {
return elementCount;
}
// 判定Vector是否为空
public synchronized boolean isEmpty() {
return elementCount == 0;
}
// 返回“Vector中全部元素对应的Enumeration”
public Enumerationelements() {
// 通过匿名类实现Enumeration
return new Enumeration() {
int count = 0;
// 是否存在下一个元素
public boolean hasMoreElements() {
return count < elementCount;
}
// 获取下一个元素
public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return (E)elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}
// 返回Vector中是否包括工具(o)
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
// 从index位置开始向后查找元素(o)。
// 若找到,则返回元素的索引值;不然,返回-1
public synchronized int indexOf(Object o, int index) {
if (o == null) {
// 若查找元素为null,则正向找出null元素,并返回它对应的序号
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
// 若查找元素不为null,则正向找出该元素,并返回它对应的序号
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
// 查找并返回元素(o)在Vector中的索引值
public int indexOf(Object o) {
return indexOf(o, 0);
}
// 从后向前查找元素(o)。并返回元素的索引
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
// 从后向前查找元素(o)。开始位置是从前向后的第index个数;
// 若找到,则返回元素的“索引值”;不然,返回-1。
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
if (o == null) {
// 若查找元素为null,则反向找出null元素,并返回它对应的序号
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
// 若查找元素不为null,则反向找出该元素,并返回它对应的序号
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
// 返回Vector中index位置的元素。
// 若index月结,则抛出异常
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return (E)elementData[index];
}
// 获取Vector中的第一个元素。
// 若失败,则抛出异常!
public synchronized E firstElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return (E)elementData[0];
}
// 获取Vector中的最后一个元素。
// 若失败,则抛出异常!
public synchronized E lastElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return (E)elementData[elementCount - 1];
}
// 配置index位置的元素值为obj
public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
// 删除index位置的元素
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; /* to let gc do its work */
}
// 在index位置处插入元素(obj)
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
// 将“元素obj”添加到Vector末端
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
// 在Vector中查找并删除元素obj。
// 乐成的话,返回true;不然,返回false。
//本栏目Vector的源码实现总体与ArrayList雷同,关于Vector的源码,给出如下几点总结:#p#分页标题#e#1、Vector有四个差异的结构要领。无参结构要领的容量为默认值10,仅包括容量的结构要领例将容量增长量(从源码中可以看出容量增长量的浸染,第二点也会对容量增长量具体说)明置为0。#p#分页标题#e#2、留意扩充容量的要领ensureCapacityHelper。与ArrayList沟通,Vector在每次增加元素(大概是1个,也大概是一组)时,都要挪用该要领来确保足够的容量。当容量不敷以容纳当前的元素个数时,就先看结构要领中传入的容量增长量参数CapacityIncrement是否为0,假如不为0,就配置新的容量为就容量加上容量增长量,假如为0,就配置新的容量为旧的容量的2倍,假如配置后的新容量还不足,则直接新容量配置为传入的参数(也就是所需的容量),尔后同样用Arrays.copyof()要领将元素拷贝到新的数组。3、许多要领都插手了synchronized同步语句,来担保线程安详。4、同样在查找给定元素索引值等的要领中,源码都将该元素的值分为null和不为null两种环境处理惩罚,Vector中也答允元素为null。5、其他许多处所都与ArrayList实现大同小异,Vector此刻已经根基不再利用。
