Incompatible Types Error in Java

后端 未结 2 821
故里飘歌
故里飘歌 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 15:00

    push method is not generic like the rest of the class, change it to:

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

    In any case the JDK ships with the class ArrayDeque which fulfill your requirements without being a piece o code pasted from a book.

    ArrayDeque stack = new ArrayDeque();
    stack.push(new YourObj());
    YourObj head = stack.peek();
    head = stack.pop();
    

提交回复
热议问题