package quiz; /** * 线程安全的栈实现,固定10容量,扩展容量没有用到 * */ public class StackImplUseArray { private Object[] objs = new Object[10];//栈元素 private int index;//栈指针 /** * 压栈操作 * */ public synchronized void push(Object obj)throws OverFlowException{ if(objs.length <= index){ throw new OverFlowException("栈空间已用完!"); } objs[index++] = obj; } /** * 出栈操作 * */ public synchronized Object pop()throws NullPointerException{ if(index == 0){ throw new NullPointerException("棧里没有元素!"); } return objs[index--]; } private void expand(){ Object[] objs = new Object[this.objs.length*2]; System.arraycopy(this.objs, 0, objs, 0, index); this.objs = objs; } }