Java-Stack栈

栈是一种数据结构,先进后出。Java中stack类是Vector的子类

  1. 入栈:public E push(E item)
  2. 出栈:public E pop()
package com.joeaaa.demo17Stack;

import java.util.Stack;

public class StackTest {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.add("A");
        stack.add("B");
        stack.add("C");
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop()); // 出现空栈异常EmptyStackException
    }
}