import java.util.EmptyStackException; public class ArrayStack implements Stack { final static int DEFAULT_SIZE = 16; private Object [] stack; private int top = -1; private int size; public ArrayStack( int sz ) { size = sz; stack = new Object[ sz ]; } public ArrayStack() { this( DEFAULT_SIZE ); } public void push(Object o) { if ( top == size-1 ) grow(); stack[ ++top ] = o; } public Object pop() throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); else { return stack[ top-- ]; } } public boolean isEmpty() { return ( top < 0 ); } public void clear() { top = -1; } private void grow() { Object [] old = stack; int oldSize = size; size *= 2; stack = new Object[ size ]; System.arraycopy( old, 0, stack, 0, oldSize ); } }