java - 不兼容的类型 : Node cannot be converted to Comparable (when passing as a parameter)

标签 java generics binary-search-tree comparable

我现在在尝试使用通用数据类型建模二叉搜索树时遇到问题。我最终将读取字符串值并将它们插入二叉树中,从而在 Nodez 类中进行字符串声明。 Nodez 类是我定义的一个类,用于声明要传递到搜索树的节点。字符串值将是此类的属性。 BSTree 基于定义如下的类:

public class BSTree<E extends Comparable<E>> implements BSTreeAPI<E>    

我的问题在于主要代码块。当我尝试插入 Nodez 类的实例时发生错误。这里的确切错误指出:“不兼容的类型:Nodez 无法转换为 Comparable”

我花了很多时间尝试调试这个,但我对泛型不太擅长?

请问有什么建议吗?谢谢!

package twotreesanalyzer;
import java.io.IOException;
import java.io.PrintStream;
import java.util.function.Function;

public class TwoTreesAnalyzer 
{

    public static class Nodez <E extends Comparable<E>> {
        public String x;
        public E node;

        public String get(){
            return x;
        }
    }

public static void main(String[] args) throws AVLTreeException, BSTreeException, IOException
    {        

        Function<String, PrintStream> printUpperCase = x -> System.out.printf("%S", x);

        BSTree bstTest = new BSTree();

        Nodez e1 = new Nodez();
        e1.x = "fresh";


        bstTest.insert(e1);

        System.out.println(bstTest.inTree(e1.get()));

    }
}

最佳答案

现在你的 BSTree 正在尝试比较你的 Nodez 对象,如果这是你想要的功能,你需要在你的 Nodez 类上实现 Comparible。我以集合树为例快速修复了它。

public static class Nodez <E extends Comparable<E>> implements Comparable<Nodez<E>>{
        public String x;
        public E node;

        public String get(){
            return x;
        }

        @Override
        public int compareTo(Nodez<E> node) {
            return node.x.compareTo(x);
        }
    }

public static void main(String[] args) throws IOException
    {        

        Function<String, PrintStream> printUpperCase = x -> System.out.printf("%S", x);

        TreeSet<Nodez<String>> bstTest = new TreeSet<>();

        Nodez<String> e1 = new Nodez<>();
        e1.x = "fresh";


        bstTest.add(e1);

        System.out.println(bstTest.contains(e1));

    }

但是我认为您希望节点能够接受任何可比较的泛型类型,在这种情况下,它的排序应该更像这样:

public static class Nodez <E extends Comparable<E>> implements Comparable<Nodez<E>>{
        public E x;
        public Nodez<E> node;

        public E get(){
            return x;
        }

        @Override
        public int compareTo(Nodez<E> node) {
            return node.x.compareTo(x);
        }
    }

public static void main(String[] args) throws IOException
    {        

        Function<String, PrintStream> printUpperCase = x -> System.out.printf("%S", x);

        TreeSet<Nodez<String>> bstTest = new TreeSet<>();

        Nodez<String> e1 = new Nodez<>();
        e1.x = "fresh";


        bstTest.add(e1);

        System.out.println(bstTest.contains(e1));
    }

关于java - 不兼容的类型 : Node cannot be converted to Comparable (when passing as a parameter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46780900/

相关文章:

java - 如何在大型软件系统中进行调试?

java - 更改 Protocol Buffer 中字段的数据类型

java - 如何将 16 位字符数组转换为 8 位字符数组并使用 JNA 发送到 C 代码?

java - 参数化类型参数?

java - 如何获取从缓冲读取器文件中获得的数字?

c# - 不带日期时间函数(如 ToShortDateString())的 DateTime 等类型的隐式运算符

java - 嵌套 Java 泛型类型

java - Java中BST的层序遍历

Java程序在BST中查找第k个最小元素

recursion - 使用 Racket 递归返回列表