Stack Api--stack in Java

Source: Internet
Author: User

A stack is a linear table that can only be inserted, fetched, or deleted at the end of the linear table. This linear table has LIFO (LIFO) feature, then how to implement this function in Java, hehe, Java has provided us with the Api--stack,stack class inherits the Vector class. Here's an example to get a look at the API:

Import Java.util.stack;public class Teststack {public static void main (string[] args) {stack<string> Stack = new Sta Ck<string> (); Stack.push ("a");//press the data onto the top of the stack, which acts addelement as stack.addelement ("B") below; Stack.push ("C"); Stack.push ("D"); Stack.push ("E"); if (!stack.empty ()) {//Determines whether the stack is empty System.out.println (Stack.peek ());//Output E. Gets the object at the top of the stack (the last item of the Vector object), but does not remove the object from the stack, and throws a Emptystackexception exception System.out.println (Stack.pop ()) If the stack is empty; Output E. Gets the object at the top of the stack (the last item of the Vector object) and removes the object from the top of the stack and throws a Emptystackexception exception System.out.println (Stack.pop ()) If the stack is empty; Output d. System.out.println (Stack.search ("C"));//Gets the position of the specified object in the stack, with a base of 1, and returns 1}} if no

Stack Api--stack in Java

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.