Understanding Stack.java from the source code

Source: Internet
Author: User

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 &LT;TT&GT ;     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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.