Java-9.8 Stack)

Source: Internet
Author: User

Java-9.8 Stack)

In this chapter, we will discuss the Stack ).

1. Features

After the first step, when an element is pressed into the stack, it will be at the bottom of the stack, and then the other will be pressed in again, built on the original element, the original element wants to go out, only when the above elements are first pushed out of the stack can there be a chance.

 

2. Method demonstration

Package com. ray. ch09; import java. util. Arrays; import java. util. Stack; public class Test {public static void main (String [] args) {Stack

 
  
Stack = new Stack

  
   
(); For (int I = 0; I <10; I ++) {stack. add (I); // In fact, The add method is not very efficient here} stack. push (12); // It is better to use push and press the stack System. out. println (Arrays. toString (stack. toArray (); System. out. println (stack. pop (); System. out. println (stack. peek (); System. out. println (Arrays. toString (stack. toArray ()));}}

  

 

 

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
12
9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Note: Stack inherits Vector, so it has all the methods of Vector, but for Stack, add, remove and other methods are full of obfuscation, it is best to use the push, pop, and peek methods for operations.

 

Summary: This chapter describes the features and notes of stacks.

 

 

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.