0

我正在实现一个可以管理堆栈的接口,我有以下代码:

public interface Stack <E>{
    public int size();
    public boolean isEmpty();
    public E top();
    public void push(E item);    //error here

实现这个接口的类有:

public class StackArray<E> implements Stack{
    private E array[];
    private int top=-1;
    public StackArray(int n){
        array=(E[]) Array.newInstance(null, n); ////not quite sure about this
    }
    //more code
    public void push(E item){
        if (top==array.length-1) System.out.println("stack is full");
        else{
            top++;
            array[top]=item;
        }
    }

我得到的错误是我没有覆盖推送方法,我看到两者都有相同的签名,但我不太确定。

我该如何解决?

4

1 回答 1

5

您没有将E参数 in绑定StackArray到inE参数Stack。使用此声明:

public class StackArray<E> implements Stack<E>
于 2013-10-13T14:44:19.763 回答