不接受Java中的参数

时间:2013-10-10 00:27:20

标签: java class parameters implements

我收到错误“类型Stack不接受参数公共类ArrayStack实现Stack”来自此代码:

public class ArrayStack<E> implements Stack<E> {

private E[] data;

private int size;

public ArrayStack() {
data = (E[])(new Object[1]);
size = 0;
}

public boolean isEmpty() {
return size == 0;
}

public Object pop() {
if (isEmpty()) {
    throw new EmptyStructureException();
}
size--;
return data[size];
}

public Object peek() {
if (isEmpty()) {
    throw new EmptyStructureException();
}
return data[size - 1];
}

protected boolean isFull() {
return size == data.length;
}

public void push(Object target) {
if (isFull()) {
    stretch();
}
data[size] = target;
size++;
}

protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]);
for (int i = 0; i < data.length; i++) {
    newData[i] = data[i];
}
data = newData;
}   
}

“类型Stack不接受参数public class ArrayStack实现Stack”

堆栈类如下。

public interface Stack<E> {

public boolean isEmpty();

public E peek();

public E pop();

public void push(E target);

}

1 个答案:

答案 0 :(得分:0)

你的peek()方法应该是这样的

    public E peek() throws EmptyStructureException {
        if (isEmpty()) {
          throw new EmptyStructureException();
        }
        return (E)data[size - 1];
    }

你的push()方法应该是这样的

   public void push(E target) {
         if (isFull()) {     
            stretch();
         }
         data[size] = target;
         size++;
   }

你的pop()方法应该是这样的

    public E pop() throws EmptyStructureException {
         if (isEmpty()) {
             throw new EmptyStructureException();
         }
         size--;
         return (E)data[size];
    }

现在您的界面如下所示

public interface Stack<E> {

      public boolean isEmpty();

      public E peek() throws EmptyStructureException;

      public E pop() throws EmptyStructureException;

      public void push(E target);

}