Stack operation, stack operation.

Source: Internet
Author: User

The stack is an advanced post-out, LIFO operation.

Sort of like a browser returning to the previous page,

 Public class Stack<e>extends vector<e>

is a subclass of a vector.

Common methods:

boolean empty()
Tests whether the stack is empty.
 E peek()
View the object at the top of the stack, but not remove it from the stack.
 E pop()
Removes the object at the top of the stack and returns the object as the value of this function.
 E push(E item)
Press the item onto the top of the stack.
 int search(Object o)
Returns the position of the object in the stack, with a base of 1.

Code:

 Packageclass set;ImportJava.util.Stack; Public classtest1{ Public Static voidMain (String args[]) {Stack<String> s =NewStack<string>() ; S.push ("A");//into the stackS.push ("B");//into the stackS.push ("C");//into the stack        if(!S.empty ()) System.out.println ("Not Empty Stack"); ElseSystem.out.println ("is empty stack"); System.out.print (s.pop () + ",") ; System.out.print (s.pop () + ",") ; System.out.println (s.pop () + ",") ;    System.out.println (S.pop ()); //there is no content at this time, error    }};

Results:

"Main" java.util.EmptyStackException at    java.util.Stack.peek (Stack.java:102)    at Java.util.Stack.pop (Stack.java:+) at    class set. Test1.main (Test1.java:16)

If there is no content in the stack, it cannot be output again.

Stack operation, stack operation.

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.