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.