栈的特性:先进后出 public class Stack<E> { private ArrayList<E> list = new ArrayList<E>(); //1、置空栈 public void empty() { list.empty(); } //2、判栈空 public boolean isEmpty() { return list.isEmpty(); } //3、入栈 public void push(E element) { list.add(element); } //4、出栈 public E pop() { E element = null; if(isEmpty()) { throw new RuntimeException("栈为空"); } //获取 element = list.get(list.size()-1); //删除元素 list.sub(list.size()-1); return element; } //5、取栈顶 public E top() { E element = null; if(isEmpty()) { throw new RuntimeException("栈为空"); } //获取 element = list.get(list.size()-1); return element; } }