The Package java.util;/** * Stack class represents a container object for last in, first out (LIFO). The stack inherits from the vector and expands five operations so that the vector can be seen as a stack. * Commonly used push and pop, and peek to get the top element of the stack, whether the test stack is empty, a search operation searches and returns its distance from the top of the stack * When first created, there are no elements in the stack * richer and more compatible LIFO operations are provided by the Deque interface, Deque is better to use than stack, for example: * deque<integer> stack = new arraydeque<integer> (); */public class Stack<e> extends vector<e> {/** * Constructs an empty stack */public stack () {}/** * Press the element to the top of the stack, and addelement (item) equivalent * @param item the item to is pushed onto this stack. * @return the <code>item</code> argument. * @see Java.util.vector#addelement */public E push (e Item) {//Call Vector addelement addelement (item); return item; /** * Remove and return the top element of the stack * @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 (); Gets the top element of the stack, obj = Peek (); remove element Removeelementat (len-1); return obj; }/** * Does not remove, only returns the top element of the stack * @return the object at the top of this stack (the last item * of the <tt& Gt 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); }/** * Test is empty * @return <code>true</code> if and only if the this stack contains * no it Ems <code>false</code> otherwise. */public Boolean empty () {return size () = = 0; /** * Returns the closest distance of the specified element to the top element of the stack, the object comparison uses the Equals method, and the top element of the stack is the coordinate start 1 * @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 isn't on the stack. */Public synchronized int search (Object o) {//Gets the subscript int i = lastIndexOf (o) of the specified element; Size-i is the distance between the top of the stack and the current element if (I >= 0) {return size ()-I; } return-1; }/** use Serialversionuid from JDK 1.0.2 for interoperability */private static final long Serialversionuid = 12244 63164541339165L;}
Understanding Stack.java from the source code