Java Implementation Stack

Source: Internet
Author: User

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

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.