简体   繁体   English

Java泛型'不兼容类型'编译时错误

[英]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. 对于CS类,我正在编写由我的教授创建的链接列表界面的链表实现。 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 {
    ...
    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. <T>引起了额外的问题。 Because Node is an inner class, it doesn't need to declare its generic type again. 因为Node是内部类,所以不需要再次声明其泛型类型。

You should declare the Node class like below: 您应该声明Node类,如下所示:

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

    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM