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.