Java Learning Note 32

Source: Internet
Author: User

The stack inherits from the Vector class, which is similar to "stack", which is a last-in, first-out (LIFO) object stack that expands the class vector by five operations


Allow the vector to be treated as a stack. It provides the usual push and pop operations, and a peek method that takes a stack vertex, whether the test stack is empty


The empty method, finds in the stack, and determines the search method to the top distance of the stack.


Let's look at a deeper understanding of the stack:



According to, we know that stack is similar to a stack, we liken the stack to a bucket, the bottom is sealed, and now we're going to object_1 the stack, and


The object_2 followed by the Object_3 and finally the Object_4, in the stack, and if we want to get the elements from the bottom, the only way


is to get the stack bottom element by first stacking the above elements sequentially, so the vector is a last-in-first-out stack.


Here's how the vector class is used:


public class Main {public static void main (string[] args) {/* * Creates an empty stack */stack stack=new stack ();/* Detects whether the current stack is empty (null returns TRU E, which returns false), the following stack is empty */system.out.println (Stack.empty ());//Next several elements are pushed into the stack by push Stack.push ("object_1"); Stack.push (" Object_2 "); Stack.push (" Object_3 "); Stack.push (" Object_4 ");//Now we look for the specified element in the stack and return the location of the element// The following output: 2system.out.println (Stack.search ("Object_3"));/* View the object at the top of the stack through peek, but not remove it from the stack. *///output: Object_4System.out.println (Stack.peek ());//output: [Object_1, Object_2, Object_3, Object_4]system.out.println ( stack);/* Removes the object at the top of the stack via pop, * and returns the object as the value of this function. *///output: Object_4System.out.println (Stack.pop ());//output: [Object_1, Object_2, object_3]system.out.println (stack);}}


Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44159585 Emotional Control _  

Java Learning Note 32

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.