1.Stack是Vector的一个子类,是先进后出(FILO)的数据结构,Stack继承了Vector的所有方法,然后扩展了属于自己的五个方法:pop(),push(),peek(),empty(),search()。
public Stack():stack的无参构造函数。
synchronized E pop():移除栈顶对象,并作为此函数的值返回该对象
synchronized E peek():查看栈顶对象,但不移除
push(E item):将对象压入栈
synchronized empty():判定栈是否为空
synchronized int search(Object o):返回对象在堆栈中的位置吗,以1位基数
可以看出pop,peek和search是 synchronized的,因为empty和push内部方法调用的Vector的方法, 而Vector是线程安全的。
2.因为Stack是继承Vector的,我们看
源码:protected Object[] elementData;
protected int elementCount
protected int capacityIncrement
private static final int MAX_ARRAY_SIZE = 2147483639
2.1 public E push(E item)
//向栈顶压入一个项
public E push(E item) {
//调用Vector类里的添加元素的方法
addElement(item);
return item;
}
public synchronized void addElement(E obj) {
//通过记录modCount参数来实现Fail-Fast机制
modCount++;
//确保栈的容量大小不会使新增的数据溢出
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
private void ensureCapacityHelper(int minCapacity) {
//防止溢出。超出了数组可容纳的长度,需要进行动态扩展!!!
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
//数组动态增加的关键所在
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//如果是Stack的话,数组扩展为原来的两倍
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
//扩展数组后需要判断两次
//第1次是新数组的容量是否比elementCount + 1的小(minCapacity;)
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//第1次是新数组的容量是否比指定最大限制Integer.MAX_VALUE - 8 大
//如果大,则minCapacity过大,需要判断下
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
//检查容量的int值是不是已经溢出
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
2.2.public synchronized E peek();
//查找栈顶对象,而不从栈中移走。
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
//Vector里的方法,获取实际栈里的元素个数
public synchronized int size() {
return elementCount;
}
public synchronized E elementAt(int index) {
if (index >= elementCount) {
//数组下标越界异常
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
//返回数据下标为index的值
return elementData(index);
}
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
2.3.public synchronized E pop()
//移走栈顶对象,将该对象作为函数值返回
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
//len-1的得到值就是数组最后一个数的下标
removeElementAt(len - 1);
return obj;
}
//Vector里的方法
public synchronized void removeElementAt(int index) {
modCount++;
//数组下标越界异常出现的情况
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
} else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
//数组中index以后的元素个数,由于Stack调用的该方法,j始终为0
int j = elementCount - index - 1;
if (j > 0) {
// 数组中index以后的元素,整体前移,(这个方法挺有用的!!)
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}
2.4.public boolean empty()
public boolean empty() {
return size() == 0;
}
2.5
// 返回栈中对象的位置,从1开始。如果对象o作为项在栈中存在,方法返回离栈顶最近的距离。
//栈中最顶部的项被认为距离为1。
public synchronized int search(Object o) {
//lastIndexOf返回一个指定的字符串值最后出现的位置,
//在一个字符串中的指定位置从后向前搜索
int i = lastIndexOf(o);
if (i >= 0) {
//所以离栈顶最近的距离需要相减
return size() - i;
}
return -1;
}
//Vector里的方法
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
//Vector、Stack里可以放null数据
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;
}
return -1;
}