数据结果 - Vector、Stack
Vector
简介
线程安全的动态数组,跟ArrayList很相似。JDK1.0中添加Vector类。Vector一样可以维护插入顺序,但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;
// 修改次数, 每次add、remove它的值都会加1(这个属性是AbstractList中的)
protected transient int modCount = 0;
// 默认最大长度
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
构造函数
public Vector(int initialCapacity, int capacityIncrement) {
super();
// 初始长度小于0时抛异常
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
// 创建元素数组
this.elementData = new Object[initialCapacity];
// 指定增长长度
this.capacityIncrement = capacityIncrement;
}
public Vector(int initialCapacity) {
// 一个参数构造函数默认指定,增长长度为0
this(initialCapacity, 0);
}
public Vector() {
// 空参构造函数,制定初始数组长度10
this(10);
}
public Vector(Collection<? extends E> c) {
// 参数集合转数组,赋值给元素数组
elementData = c.toArray();
// 设置元素个数
elementCount = elementData.length;
// 不是Object数组时转为Object数组
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
从构造函数中可以看出创建时就初始化了10个长度的元素数组,增长长度只有第一个构造函数有设置值,其他的都为0
基础方法
public synchronized int size() {
// 返回元素个数
return elementCount;
}
public synchronized boolean isEmpty() {
// 元素个数为0时返回true
return elementCount == 0;
}
设置长度
public synchronized void setSize(int newSize) {
// 修改次数加1
modCount++;
// 判断是否需要扩容
if (newSize > elementCount) {
ensureCapacityHelper(newSize);
} else {
// 参数以后的元素清空掉
for (int i = newSize ; i < elementCount ; i++) {
elementData[i] = null;
}
}
elementCount = newSize;
}
private void ensureCapacityHelper(int minCapacity) {
// 判断新长度是否大于原数组长度,大于则扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
新长度小于元素个数时阶段原数组,新长度大于元素数组长度时使用新长度扩容
扩容
private void grow(int minCapacity) {
// 获取原长度
int oldCapacity = elementData.length;
// 获取新长度
// 增长长度不为0时 新长度=原长度+增长长度
// 增长长度不为0时 新长度=原长度+增长长度
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
// 判断新长度是否够用,不够用使用参数
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
// 判断新长度是否越界
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
// 长度小于0则抛异常
if (minCapacity < 0)
throw new OutOfMemoryError();
// 长度大于默认最大值取Integer最大值
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
Vector扩容比较特殊,不像ArrayList,ArrayList没有增长长度正常情况下扩容只能为原长度的1.5倍,而Vector在扩容时,先判断增长长度是否为0,不为0时使用增长长度,为0时原长度乘以2
查找元素
从前往后找
public int indexOf(Object o) {
return indexOf(o, 0);
}
public synchronized int indexOf(Object o, int index) {
if (o == null) {
// index为起始位置
for (int i = index ; i < elementCount ; i++)
// 查找元素为空
if (elementData[i]==null)
return i;
} else {
// index为起始位置
for (int i = index ; i < elementCount ; i++)
// 查找equals一样的元素
if (o.equals(elementData[i]))
return i;
}
// 没有找到返回-1
return -1;
}
从后往前找
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
public synchronized int lastIndexOf(Object o, int index) {
// 判断index是否超过元素个数
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
if (o == null) {
// 从后往前找
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
// 从后往前找
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
// 没有找到返回-1
return -1;
}
元素数组操作
public Enumeration<E> elements() {
// 每次调用此方法都会有一个新对象
return new Enumeration<E>() {
int count = 0;
public boolean hasMoreElements() {
return count < elementCount;
}
public E nextElement() {
// 虽然方法上没加锁,但是这儿加了,锁的是当前对象
synchronized (java.util.Vector.this) {
if (count < elementCount) {
return elementData(count++);
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}
这里顺带介绍Enumeration,Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素。这种传统接口已被迭代器取代,虽然Enumeration 还未被遗弃,但在现代代码中已经被很少使用了。
前后第一个元素
public synchronized E firstElement() {
// 数组为空时抛异常
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(0);
}
public synchronized E lastElement() {
// 数组为空时抛异常
if (elementCount == 0) {
throw new NoSuchElementException();
}
// 取最后一个元素
return elementData(elementCount - 1);
}
索引找元素
E elementData(int index) {
// 通过索引直接从数组中取值
return (E) elementData[index];
}
public synchronized E elementAt(int index) {
// 判断索引是否大于元素个数
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData(index);
}
public synchronized void setElementAt(E obj, int index) {
// 超过元素个数抛异常
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
// 根据索引设值
elementData[index] = obj;
}
删除指定位置上的元素
public synchronized void removeElementAt(int index) {
modCount++;
// 判断指定索引是否超过元素个数
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
// 索引不能小于0
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
// 索引位置以后的元素前向前移动
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
// 元素个数减1
elementCount--;
// 最后一个元素置空
elementData[elementCount] = null; /* to let gc do its work */
}
在指定位置上插入元素
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++;
}
追加元素
public synchronized void addElement(E obj) {
modCount++;
// 判断是否需要扩容
ensureCapacityHelper(elementCount + 1);
// 末尾增加元素
elementData[elementCount++] = obj;
}
删除元素
public synchronized boolean removeElement(Object obj) {
modCount++;
// 找到元素位置
int i = indexOf(obj);
// 存在就删除
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
删除所有元素
public synchronized void removeAllElements() {
modCount++;
// 遍历索引元素,依次置空
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
添加删除
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
public boolean remove(Object o) {
return removeElement(o);
}
添加删除方法主要是还使用上面的方法,这里就不在赘述
沿用父类方法
public synchronized boolean containsAll(Collection<?> c) {
return super.containsAll(c);
}
public synchronized boolean removeAll(Collection<?> c) {
return super.removeAll(c);
}
public synchronized boolean retainAll(Collection<?> c) {
return super.retainAll(c);
}
public synchronized boolean equals(Object o) {
return super.equals(o);
}
public synchronized int hashCode() {
return super.hashCode();
}
public synchronized String toString() {
return super.toString();
}
父类中的方法参照List接口篇
内部类
private class Itr implements Iterator<E>
final class ListItr extends java.util.Vector.Itr implements ListIterator<E>
static final class VectorSpliterator<E> implements Spliterator<E>
Stack
简介
栈是一种只能在一端进行插入或删除操作的线性表,先进后出表
类
public class Stack<E> extends Vector<E>
注意继承自Vector
public E push(E item) {
addElement(item);
return item;
}
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
public synchronized E peek() {
int len = size();
// 判断长度
if (len == 0)
throw new EmptyStackException();
// 调用Vector中的方法
return elementAt(len - 1);
}
自有方法很少,主要的就入栈出栈