开发者

Java Generics 'Incompatible Type' Compile-Time Error

For a CS class I am writing a linked list implementation of a linked list interface created by my professor. The assignment requires us to use generics for the list. What I have created, I think, is pretty standard.

public class MyLinkedList<T> implements ADTListInterface {
  开发者_Go百科  ...
    private class Node<T> {
        Node<T> head;
        Node<T> prev;
        public Node(int max) {

        ...

        }

        public void shift() {
            ...
            Node<T> newNode = new Node<T>(this.max);
            newNode.prev = head.prev;
            ...
        }

    }

    ...

}

At compile time following error is generated:

MyLinkedList.java:111: incompatible types
   found   : MyLinkedList<T>.Node<T>
   required: MyLinkedList<T>.Node<T>
newNode.prev = head.prev;

This error has me very confused. Can anyone explain to me what the issue is?


Here is probably the problem:

private class Node<T> {

The <T> is causing extra problems. Because Node is an inner class, it doesn't need to declare its generic type again.

You should declare the Node class like below:

public class MyLinkedList<T> implements ADTListInterface {
...
private class Node {
    Node head;
    Node prev;
    public Node(int max) {

    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜