Introduction to the Java.util.Stack class

Source: Internet
Author: User

The stack is a last-in, first-out (last-in-Out,lifo) heap, extending 5 methods based on the vector class

Deque (double-ended queue) has better integrity and consistency than stack and should be used preferentially

    1. E push (e Item) presses the item onto the top of the stack.
    2. E pop () removes the object at the top of the stack and returns the object as the value of this function.
    3. E peek () looks at the object at the top of the stack, but does not remove it from the stack.
    4. The Boolean empty () test stack is empty.
    5. An int search (object o) returns the position of the object in the stack, with a base of 1.

The stack itself is extended by vector, and the vector itself is an array of objects that can grow (a growable array of objects) so where does this array stack top, as Stack bottom?

The answer can only be found in the source code, jdk1.6:

    1. Public class Stack<e> extends vector<e> {
    2. /**
  • * Creates an empty Stack. */
  • Public Stack () {
  • }
  • /**
  • * Pushes an item onto the top of this stack. This have exactly
  • * The same effect as:
  • * <blockquote><pre>
  • * AddElement (item) </pre></blockquote>
  • *
  • * @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) {
  • AddElement (item);
  • return item;
  • }
  • /**
  • * Removes the object at the top of this stack and returns that
  • * Object as the value of this function.
  • *
  • * @return The object at the top of this stack (the last item
  • * of the <tt>Vector</tt> object).
  • * @exception emptystackexception If this stack is empty.
  • */
  • Public synchronized E pop () {
  • E obj;
  • int len = size ();
  • obj = Peek ();
  • Removeelementat (Len- 1);
  • return obj;
  • }
  • /**
  • * Looks at the object on the top of this stack without removing it
  • * from the stack.
  • *
  • * @return The object at the top of this stack (the last item
  • * of the <tt>Vector</tt> object).
  • * @exception emptystackexception If this stack is empty.
  • */
  • Public Synchronized E Peek () {
  • int len = size ();
  • if (len = = 0)
  • Throw new Emptystackexception ();
  • Return ElementAt (Len- 1);
  • }
  • /**
  • * Tests If this stack is empty.
  • *
  • * @return <code>true</code> if and only if the 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 being at distance <tt>1</tt>. The <tt>equals</tt>
  • * method is used-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 isn't on the stack.
  • */
  • 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;
  • }
  • Note the object at the top of the "This" stack (the last item of the vector object, which can be found in the array (vector) at the top of the stack by using the Peek () method
  • The pop, peek, and search methods themselves are synchronized

    The push method invokes the AddElement method of the parent class

    The empty method invokes the size method of the parent class

    Vector class for thread-safe classes

    In conclusion, thestack class is a thread-safe class (the data inconsistency problem resulting from multiple method invocations is a category of atomicity)

    • Public class Test {
    • Public static void Main (string[] args) {
    • stack<string> s = new stack<string> ();
  • SYSTEM.OUT.PRINTLN ("------isEmpty");
    • System.out.println (S.isempty ());
    • SYSTEM.OUT.PRINTLN ("------push");
    • S.push ("1");
    • S.push ("2");
    • S.push ("3");
    • Test.it (s);
    • SYSTEM.OUT.PRINTLN ("------pops");
    • String str = S.pop ();
    • System.out.println (str);
    • Test.it (s);
    • SYSTEM.OUT.PRINTLN ("------peek");
    • str = S.peek ();
    • System.out.println (str);
    • Test.it (s);
    • SYSTEM.OUT.PRINTLN ("------search");
    • int i = S.search ("2");
    • System.out.println (i);
    • i = S.search ("1");
    • System.out.println (i);
    • i = S.search ("none");
    • System.out.println (i);
    • }
    • Public static void it (stack<string> s) {
    • System.out.print ("iterator:");
    • Iterator<string> it = S.iterator ();
    • while (It.hasnext ()) {
    • System.out.print (It.next () +";");
    • }
    • System.out.print ("\ n");
    • }
    • }
      • Results:
        • ------IsEmpty
        • True
        • ------Push
        • iterator:1;2;3;
        • ------Pop
        • 3 --The top of the stack is the last of the array
        • iterator:1;2;
        • ------Peek
        • 2 --pop After the deletion, peek only take not delete
        • iterator:1;2;
        • ------Search
        • 1 --base 1, which is 1 of the top of the stack
        • 2 --the distance from the top of the stack is 2-1=1
        • -1 --there is no Yu Ching
        • The stack does not require the uniqueness of the data to be saved, and when there are multiple identical item in the stack, the search method is called, returning only the object that is equal to the top of the stack and the distance between the item and the top of the stack (see the Search method description in the source code)

Introduction to the Java.util.Stack class

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.