Java Improvement Chapter (31)-----Stack

Source: Internet
Author: User

In Java, the Stack class represents the last-in, first-out (LIFO) object stacks. Stack is a very common data structure, it adopts the typical advanced after-out operation mode completed. Each stack includes a stack top, each time the stack is out of the top of the data. For example, the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2hlbnnzeq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

The stack expands the vector by five operations, agreeing to treat the vector as a stack. This five operations such as the following:

Operation Description

empty()

Whether the test stack is empty.

peek()

View the object at the top of the stack, but not remove it from the stack.

pop()

Removes the object at the top of the stack and returns the object as the value of this function.

push(E item)

Press the item onto the top of the stack.

search(Object o)

Returns the position of the object in the stack, with a base of 1.

Stack inherits vectors. He made a simple expansion of vectors:

public class Stack<e> extends vector<e>

The implementation of the stack is very easy, there is only one construction method, five implementations (the method inherited from the vector does not count), at the same time the actual source code is very easy

/** * constructor */public stack () {}/** * Push function: put element on top of stack */public E push (E Item) {//Add meta        The top of the stack.        The reality of AddElement () is now Vector.java in AddElement (item);    return item;        }/** * Pop function: Returns the top element of the stack and removes it from the stack */public synchronized e pop () {e obj;        int len = size ();        obj = Peek (); Deletes the top element of the stack.        The reality of Removeelementat () is now Vector.java in Removeelementat (len-1);    return obj; }/** * Peek function: Returns the top element of the stack.        Do not run delete operation */public synchronized E peek () {int len = size ();        if (len = = 0) throw new emptystackexception (); Returns the top element of the stack.    ElementAt () detailed real now Vector.java return ElementAt (len-1);    }/** * Whether the stack is empty */public Boolean empty () {return size () = = 0; /** * Find the position of "element O" in the stack: number of top-of-stack from the bottom of the stack */public synchronized int search (Object o) {//Get element index.        ElementAt () detailed real now Vector.java int i = lastIndexOf (o);  if (I >= 0) {          return size ()-I;    } return-1; }

Many of the stack's source code are based on vectors. So here is no longer a statement of exhaustion. For many other details, please refer to: Java Improvement (29)-----Vector

Java Improvement Chapter (31)-----Stack

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.