代码之家  ›  专栏  ›  技术社区  ›  boppa

不兼容的类型:节点无法转换为可比较(当作为参数传递时)

  •  0
  • boppa  · 技术社区  · 7 年前

    我现在在尝试用通用数据类型建模二叉搜索树时遇到了问题。我最终将读取字符串值并将其插入到二叉树中,从而在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()));
    
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   luckydog32    7 年前

    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));
        }