import java.util.EmptyStackException;
import java.util.Stack;
public class ArrayStack<E> implements TestStack<E>{
private E[] theArray;
private int topOfStack;
public static final int DEFAULT_CAPACITY = 20;
public ArrayStack(){
theArray = (E[])new Object[DEFAULT_CAPACITY];
topOfStack = -1;
}
//overwrite the push meathod
public void push(E data){
if (topOfStack == theArray.length-1);
{
doubleArray();
}
topOfStackPP;
theArray[topOfStack]=data;
}
@Override
public E pop() throws EmptyStackException {
if(empty()){
throw new EmptyStackException("Stack pop");
}
E result = theArray[topOfStack];
topOfStack--;
return result;
}
@Override
public E peek() throws EmptyStackException {
if (empty()){
throw new EmptyStackException("Stack peek");
}
return theArray[topOfStack];
}
@Override
public boolean empty() {
return topOfStack == -1;
}
@Override
public int size() {
return topOfStack + 1;
}
private void doubleArray(){
E[] temp =theArray;
theArray = (E[]) new Object[temp.length*2];
for(int i=0; i<temp.length;iPP){
theArray[i]=temp[i];
}
}
}
in throw new EmptyStackException ("Stack peek")
here and above. ("Stack pop")
are all reporting errors, and the error says EmptyStackException () in EmptyStackException cannot be applied to..
. This is very strange. I don"t know what the error is,