Incompatible Types Error in Java

后端 未结 2 834
故里飘歌
故里飘歌 2021-01-23 14:24

I keep receiving an error that says that there are incompatible types. I copied this directly out of a book because we are supposed to make changes to the code to enhance the ga

2条回答
  •  我在风中等你
    2021-01-23 14:35

    Change Object to E as the push() method's parameter type.

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

    Likewise, you should also change the declare return type of pop() and peek() to E.

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

    Now your class is fully generic.

提交回复
热议问题