/**
* The <code>Stack</code> class represents a last-in-first-out
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
* operations that allow a vector to be treated as a stack. The usual
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
* method to <tt>peek</tt> at the top item on the stack, a method to test
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
* the stack for an item and discover how far it is from the top.
* <p>
*/
Stack类表示对象的后进先出(LIFO)堆栈。 它通过五个操作扩展了Vector类,这些操作允许将矢量视为堆栈。 提供了通常的push()和pop()操作,以及在堆栈顶部的peek()方法,一种用于测试的方法堆栈是否为空,以及搜索某个项目的堆栈并发现其距顶部有多远的方法。
/**
* When a stack is first created, it contains no items.
*/
首次创建堆栈时,它不包含任何项目。
/**
* <p>A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre> {@code
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
*
* @author Jonathan Payne
* @since JDK1.0
*/
Deque接口及其实现提供了一组更完整和一致的LIFO堆栈操作,应优先使用此类。
public class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}
/**
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
将项目推入此堆栈的顶部。 这与以下内容完全相同:addElement(item)
public E push(E item) {
addElement(item);
return item;
}
/**
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
删除此堆栈顶部的对象,并将该对象作为此函数的值返回。
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
/**
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
在不将其从堆栈中移除的情况下,查看该堆栈顶部的对象。
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
/**
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
测试此堆栈是否为空。
public boolean empty() {
return size() == 0;
}
/**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
返回对象在此堆栈上的从1开始的位置。
如果对象o作为该堆栈中的一个项目出现,则此方法返回到距堆栈顶部最近的堆栈顶部的距离; 堆栈中最顶层的项目被视为距离1。equals()方法用于将o与该堆栈中的项目进行比较。
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}