Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 树数据结构(泛型)_Java_Generics_Tree - Fatal编程技术网

Java 树数据结构(泛型)

Java 树数据结构(泛型),java,generics,tree,Java,Generics,Tree,我正在实现一个简单的树数据结构。 我的目标是在树类中创建Node类型的对象。 在这种情况下,我不理解如何管理泛型 这是我的精简树结构: public class RBT<K extends Comparable<K>, V> { Node<K,V> a = null; Node<Integer,String> b = null; public RBT() { this.a = new Node<K,

我正在实现一个简单的树数据结构。 我的目标是在树类中创建Node类型的对象。 在这种情况下,我不理解如何管理泛型

这是我的精简树结构:

public class RBT<K extends Comparable<K>, V> {

    Node<K,V> a = null;
    Node<Integer,String> b = null;

    public RBT() {
        this.a = new Node<K,V>(new Integer(1), new String("node1")); // ERROR
        this.b = new Node<Integer, String>(new Integer(1), new String("node1"));

        this.a = this.b; // ERROR!
    }

    public void newNode(K key, K value) {
        Node<K,V> test = new Node<K,V>(key, value); // ERROR
    }

    public static void main(String[] args) {
        RBT<Integer,String> rbt = new RBT<Integer,String>();
        rbt.newNode(new Integer(2), new String("node2")); // ERROR!
    }
}
公共类RBT{
节点a=null;
节点b=null;
公共RBT(){
this.a=新节点(新整数(1),新字符串(“node1”);//错误
this.b=新节点(新整数(1),新字符串(“node1”);
this.a=this.b;//错误!
}
public void newNode(K键,K值){
节点测试=新节点(键、值);//错误
}
公共静态void main(字符串[]args){
RBT RBT=新RBT();
newNode(新整数(2),新字符串(“node2”);//错误!
}
}
这是我的节点表示:

public class Node<K,V> {
    private Node<K,V> father;
    private Node<K,V> left;
    private Node<K,V> right;

    private K key;
    private V value;

    Node(K key, V value) {
        this.father = this.left = this.right = null;

        this.key = key;
        this.value = value;
    }
}
公共类节点{
私有节点父节点;
私有节点左;
私有节点权;
私钥;
私人价值;
节点(K键,V值){
this.father=this.left=this.right=null;
this.key=key;
这个值=值;
}
}

提前感谢您的帮助。

您已经发现,您无法写作

new Node<K, V>
新节点
您必须指定类型

Node<Integer, String> node = new Node<Integer, String>(new Integer(1), new String("anything"))
Node节点=新节点(新整数(1)、新字符串(“任何内容”))

我是Java泛型的noob。你可以这样做

public class RBT<K extends Comparable<K>, V> {

    Node<K,V> a = null;
    Node<Integer,String> b = null;

    public RBT() {
        this.a = new Node(new Integer(1), new String("node1")); // ERROR
        this.b = new Node<Integer, String>(new Integer(1), new String("node1"));

        this.a = (Node<K, V>) this.b; // ERROR!
    }

    public void newNode(K key, V value) {
        Node<K,V> test = new Node(key, value); // ERROR
    }

    public static void main(String[] args) {
        RBT<Integer,String> rbt = new RBT<Integer,String>();
        rbt.newNode(new Integer(2), new String("node2")); // ERROR!
    }
}
公共类RBT{
节点a=null;
节点b=null;
公共RBT(){
this.a=新节点(新整数(1),新字符串(“node1”);//错误
this.b=新节点(新整数(1),新字符串(“node1”);
this.a=(节点)this.b;//错误!
}
public void newNode(K键,V值){
节点测试=新节点(键、值);//错误
}
公共静态void main(字符串[]args){
RBT RBT=新RBT();
newNode(新整数(2),新字符串(“node2”);//错误!
}
}
请原谅我糟糕的英语。我不是以英语为母语的人。

首先

this.a = new Node<K,V>(new Integer(1), new String("node1")); // ERROR
第二,

this.a = this.b; // ERROR!
this.a
具有泛型
,而
this.b
具有
。因此,存在一个不匹配,可以通过强制转换来解决,因为您确切地知道
K,V
将是
Integer,String

this.a = (Node<K, V>) this.b;
this.a=(节点)this.b;

这里你的方法签名也是错误的

public void newNode(K key, K value) {
                           ^ should be V
        Node<K,V> test = new Node(key, value); // ERROR
    }
public void newNode(K键,K值){
^应该是V
节点测试=新节点(键、值);//错误
}

K
V
实际上是他使用它们的地方的“类型”。。。问题是他没有将这些类型的对象作为参数传递…这可能会生成一个
警告:[未选中]未选中强制转换
@JohnMòf是的,编译器会生成一个警告,但不会影响结果。我不知道如何修复该警告,除了在其上方添加SuppressWarning。对不起,我调查了更多,你是对的。谢谢你的帮助。谢谢@Murat,我用你的建议替换了代码,但是
this.a=(RBT.Node)this.b
抛出以下错误:
找不到符号:this.a=(RBT.Node)this.b;符号:类节点,位置:类RBT
最后您的解决方案类似于@Yi Zhang的代码。在构造函数签名之前添加
@SuppressWarnings(“unchecked”)
,绕过这个问题,非常感谢。但我还是不相信。@JohnMòf这不是编译器的问题。编译器告诉您正在执行未经检查的强制转换,因为您知道
K,V
将是
String,Integer
,但编译器没有。最后,我找到了另一个有趣的解决方案。我已经在RBT类
私有类节点{}
内将节点类声明为私有(注意,我省略了)。这可以简化泛型的管理。
public void newNode(K key, K value) {
                           ^ should be V
        Node<K,V> test = new Node(key, value); // ERROR
    }