Stack是什么?
Stack是基于数组实现的栈结构,具有先进后出的特性
继承结构
继承了Vector
public class Stack<E> extends Vector<E> {
}
构造方法
public Stack() {
}
域
private static final long serialVersionUID = 1224463164541339165L;
推
调用父类Vector.addElement()将数据添加到栈顶(尾部)
public E push(E item) {
addElement(item);
return item;
}
private static final long serialVersionUID = 1224463164541339165L;
查看
调用父类Vector.removeElementAt()获取栈顶(尾部)元素
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
拉
调用peek()获取栈顶返回,然后调用父类Vector.removeElementAt()删除栈顶(尾部)元素
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
判空
public boolean empty() {
return size() == 0;
}
查
从栈顶往下(从后往前)查找,找到后返回其距离栈顶的位置
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}