为对象创建堆栈类
import java.util.*;/*来自 n o w j a v a . c o m - 时代Java*/ class Stack { private List<Object> items; public Stack(int size) { items = new ArrayList<Object>(); } public void push(Object item) { items.add(item); } public Object pop() { if (items.size() == 0) throw new EmptyStackException(); return items.remove(items.size() - 1); }/**来自 时代Java公众号 - N o w J a v a . c o m**/ public boolean isEmpty() { return items.isEmpty(); } } public class StackTest { public static void main(String[] args) { Stack s = new Stack(3); s.push("hi"); s.push("hello"); s.push("good day"); System.out.format("%s%n", s.pop()); System.out.format("%s%n", s.pop()); System.out.format("%s%n", s.pop()); } }