Stack (Stack)----Java class set
The stack is a very important data structure, which can be reflected everywhere in our daily life and in the operating system of the computer.
Such as taking a plate and placing a plate, opening a folder and returning to the previous layer, returning to a web page, putting on and undressing, etc., there are stacks everywhere.
The basic principle of stack:
The stack (Stack) stores data in the form of last-in-first-out (Last Int First Out, FIFO).
The stack data structure is also implemented in the Java collection. The main methods are as follows:
simple small case
package chapter_thirteen;
import com.sun.corba.se.spi.orbutil.fsm.FSMTest;
import java.util.Stack;
public class StackDemo {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
stack.push("Hello"); //使用push()方法将字符串压入栈中
stack.push("World");
stack.push("Deku");
System.out.println("栈顶元素为---->" + stack.peek()); //取出栈顶元素
stack.push("For One.");
System.out.println("此时栈顶元素为---->" + stack.peek()); //添加新数据后再次得到栈顶元素
stack.pop();
stack.pop();
System.out.println("现在栈顶元素为---->" + stack.peek()); //弹出两个元素后得到的栈顶元素
}
}
operation result
栈顶元素为---->Deku
此时栈顶元素为---->For One.
现在栈顶元素为---->World