声明类型为T的新对象

我正在尝试制作一个二叉搜索树,这个类看起来像这样:

public class Node<T extends Comparable<T>> {
    
    T data;
    Node<T> left
    Node<T> right;
    int height;
    
    public Node(T data) {
        this.data = data;
        this.left = null;
        this.right = null;
        this.height = 0;
    }
    
}

我在其中声明对象的客户端程序如下所示:

public static void main(String[] args) {
    
    Node<T> a = new Node<T>(8);
    
}

然后错误出现,因为构造函数Node<T>(T)引用了缺少的类型 T。我想知道如何将 T 数据声明为 8 的 int 类型。如何在不更改 Node 类的情况下做到这一点?

回答

尝试

Node<Integer> a = new Node<>(8);

进一步阅读:https : //docs.oracle.com/javase/tutorial/java/generics/types.html


以上是声明类型为T的新对象的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>