The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top. Stack类代表后进先出(LIFO)的栈对象。它继承自类Vector,提供了5种操作,使 vector能够作为栈处理。除了提供常规的push和pop操作外,还同时提供方法来查找栈顶项, 测试栈是否为空,查找栈中某项相对于栈顶的距离。
When a stack is first created, it contains no items. 当栈第一次被创建时,它不含任何项。
Returns the 1-based position where an object is on this stack. If the object o 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 1. The equals method is used to compare o to the items in this stack. 返回栈中对象的位置,从1开始。如果对象o作为项在栈中存在,方法返回离栈顶最近的距离。 栈中最顶部的项被认为距离为1。equals方法用来比较o和栈中的项。
Parameters:
o - the desired object. 期望对象。
Returns:
the 1-based position from the top of the stack where the object is located; the return value -1 indicates that the object is not on the stack. 栈中对象的位置,从1开始。返回-1值表示对象不在栈中。
Submit a bug or feature
For further API reference and developer documentation, see
Java 2 SDK SE Developer Documentation
. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.