我现在有问题,试图用通用数据类型对二叉搜索树建模。我最终将读取字符串值并将它们插入二进制树中,因此Nodez类中的字符串声明。 Nodez类是我定义的用于声明节点传递给搜索树的类。字符串值将是该类的一个属性。 BSTree基于以下类定义:不兼容的类型:节点无法转换为Comparable(当作为参数传递时)
public class BSTree<E extends Comparable<E>> implements BSTreeAPI<E>
我的问题在于主代码块。当我尝试插入Nodez类的一个实例时发生错误。这里的确切的错误说:“不兼容的类型:Nodez不能转换为可比”
我花了很多时间试图调试一个很好的数额,但我没有泛型如此之大?
有什么建议吗?谢谢!
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()));
}
}
以及如何类' Nodez'看起来像?它具有可比性吗?顺便说一下,为你的类/方法/对象提供好的名字是一个好主意,即使它只是一个玩具项目! – alfasin
对。 Nodez被声明在main之上。当它实现了媲美,它返回一个指出nodez不是抽象的,在可比 – boppa