/**
 * 使用数组实现固定大小的栈结构
 */
public class ArrayStack {
    private int[] data;
    private int index;

    public ArrayStack(int capacity){
        if (capacity < 0)
            throw new IllegalArgumentException("the capacity should not be less than 0");
        data = new int[capacity];
        index = 0;
    }

    public void push(int obj){
        if (index == data.length)
            throw new ArrayIndexOutOfBoundsException("the stack is full");
        data[index++] = obj;
    }

    public int pop(){
        if (index == 0)
            throw new ArrayIndexOutOfBoundsException("the stack is empty");
        return data[--index];
    }

    public Integer peek(){
        if (index == 0)
            return null;
        return data[index-1];
    }
}