Stack只继承了Vector类,通过Vector底层数组实现栈功能
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}
//省略其他函数
}
peek()函数实现
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
poll()函数实现
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
push()函数实现
public E push(E item) {
addElement(item);
return item;
}