Vector
1.构造
1.1Vector()
public Vector() {
this(10); //默认初始容量为10
}
1.2Vector(int)
public Vector(int initialCapacity) {
this(initialCapacity, 0); //直接给初始容量,还有容量增长值。
}
1.3Vector(int,int)
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0) //不符合条件,抛异常
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity]; //将带有指定容量的数组赋值给elementData数组
this.capacityIncrement = capacityIncrement; //将指定容量增长系数赋给数组
}
1.4Vector(Collection<? extends E>)
public Vector(Collection<? extends E> c) {
elementData = c.toArray(); //把数组赋给集合
elementCount = elementData.length; //把数组长度赋给当前数组的elementCount
//本身一直有初始容量,不需要判断是否为空
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class) //判断对象是否一致
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); //不是同意类型要转换为Object
}
2.增
2.1add(E)
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1); //保证数组容量够用,不够就扩容
elementData[elementCount++] = e; //给数组末尾赋值,再给elementCount加一
return true; //添加成功
}
2.2add(int,E)
public void add(int index, E element) {
insertElementAt(element, index);
}
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++; //个数加一
}
3.删
3.1remove(Object)
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj); //找到元素位置
if (i >= 0) {
removeElementAt(i); //判断位置合理性并进行元素复制移动
return true;
}
return false;
}
3.2remove(int)
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; //返回删除的元素
}
4.改
set(int,E)
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index); //都到该位置旧值
elementData[index] = element; //赋新值
return oldValue; //返回被替换的值
}
5.查
get(int)
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index); //得到该位置的值
}
6.扩容
grow(minCapacity)
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? //容量增长系数>0时,就加增值,否则数组长度翻两倍
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0) //如果新的数组容量小于传入的参数要求的最小容量,取minCapacity
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity); //把旧数组elementData放入新数组
}
采用什么数据结构?
如果查找一个指定位置的数据,vector和arraylist使用的时间是相同的,都是0(1),这个时候使用vector和arraylist都可以。而如果移动一个指定位置的数据花费的时间为0(n-i)n为总长度,这个时候就应该考虑到使用linklist,因为它移动一个指定位置的数据所花费的时间为0(1),而查询一个指定位置的数据时花费的时间为0(i)
如何实现的?为什么这么实现?
线程是否安全?
Vector中的操作是线程安全的,里面的增删改查方法前都加了synchronized关键字