run time error running the stack which could be due to array size
<p>I created a class that runs a stack. However when I run the main class it gives a runtime error. The class runs like a stack but when the numbers entered go over the stack size its supposed to call a method that copies the array  add more size to the stack.</p>

<p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at IntegerStack.push(IntegerStack.java:26)
at Lab15.main(Lab15.java:18)</p>

<p>I think the problem lies within this class I dont know exactly where my problem lies. I could be wrong but it could be with my copy of the array. </p>

<pre><code>            import java.util.Arrays;


         public class IntegerStack 

{

private int stack [];
private int top; 


public IntegerStack(int SIZE) 
{
    stack = new int [SIZE];
    top = -1;
}

public void push(int i) 
{
    if (top == stack.length)

        extendStack();

    else

       stack[++top]= i;

}

public int pop() 
{

    return stack[top--];
}

public int peek()
{

    return stack[top];  

}


public boolean isEmpty() 
{
    if ( top == -1)

        return true;
    else    
        return false;

}

private void extendStack()
{

    stack = Arrays.copyOf(stack, 2 * stack.length);
}

 }
</code></pre>
