Vector:底层数组,查询快增删慢,线程安全
参考:https://www.w3cschool.cn/java/java-vector-class.html
Vector属性
protected Object[] elementData; //数组
protected int elementCount; //元素个数
protected int capacityIncrement; //向量容量自动增加的量
构造方法
Vector类支持4种构造方法
第一种构造方法创建一个默认的向量,默认大小为10:
Vector()
底层源码:
调用第二种构造 默认大小为10:
第二种构造方法创建指定大小的向量。
Vector(int size)
调用第三种构造,增量默认0
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
第三种构造方法创建指定大小的向量,并且增量用incr指定. 增量表示向量每次增加的元素数目。
Vector(int size,int incr)
底层源码:
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity]; //创建一个数组,
this.capacityIncrement = capacityIncrement; //数组增量
}
第四中构造方法创建一个包含集合c元素的向量:
Vector(Collection c)
底层源码:
public Vector(Collection<? extends E> c) {
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);
}
方法:
1,在数组尾部添加一个元素 (请注意 它的方法里面是使用了 synchronized的)
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
//扩容
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity); //如果增量 capacityIncrement小于0 则每次扩容 之前的两倍
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
2.移除指定元素:
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 int indexOf(Object o) {
return indexOf(o, 0);
}
3.从指定位置开始 获取元素位置
public synchronized int indexOf(Object o, int index) {
if (o == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
4,移除指定位置的元素
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 */ //最后一位置空
}
Stack :
Stack 继承 Vector 它实现了一个标准的后进先出的栈 .(注意内部实现,压入栈顶,实际上是在数组末尾添加)
参考: https://www.runoob.com/java/java-stack-class.html
boolean empty() 判断堆栈是否为空。
Object peek( ) 查看栈顶对象 (获取数组长度-1位置的元素,即最后一位)
Object pop( ) 移除栈顶对象, 并返回此对象 (也是数组最后一位的元素)
Object push(Object element) 把元素压入堆栈顶部。 使用addElement(E obj) 方法 类似add(E e)