使用固定大小的数组实现堆栈。
import static java.lang.System.out; /** from 时代Java公众号**/ public class FixedCapacityStack<Item> { private Item[] data; private int N; @SuppressWarnings("unchecked") public FixedCapacityStack(int capacity) { data = (Item[]) new Object[capacity]; //work around } public boolean isEmpty() { return N == 0; }/**来 自 NowJava.com**/ public void push(Item item) { data[N++] = item; } public Item pop() { Item item = data[--N]; data[N] = null; return item; } public static void main(String[] args) { FixedCapacityStack<Integer> numbers = new FixedCapacityStack<Integer>(5); numbers.push(22); numbers.pu