1 PackageTest;2 ImportJava.util.*;3 Public classStack_test {4 5 Public Static voidMain (string[] args) {6 //TODO Auto-generated method stubs7 //input and character processing8Scanner Scanner =NewScanner (system.in);9String string =scanner.nextline ();Tenstring[] string2 = String.Split (""); One //Statement Astack<string> stack =NewStack<string>(); - //Pushing character push into the stack - for(inti = 0; i < string2.length; i++) { the Stack.push (String2[i]); - } - //Output -System.out.println ("Stack is:" +stack); + //pop out of the stack -System.out.println ("Pop:" +Stack.pop ()); +System.out.println ("Stack is:" +stack); A } at -}
The output is:
Abcdefgstack is: [A, B, C, D, E, F, g]pop:gstack are: [A, B, C, D, E, F]
In the code:
1 New Stack<string> ();
To declare the stack, import java.util.Stack is required;
After the stack has been built, there are several ways to use it.
empty ()
A Boolean type that is used to test whether the stack is empty. True is null, FALSE is not empty.
Peek ()
View the top element of the stack, but do not delete it.
Pop ()
Returns the top element of the stack, deleting the element.
Push ()
Push elements into the stack
Search (Object o)
Returns the position of the element to find, and the return value is of type int.
Java Implementation Stack