Java 实现通用节点
当我尝试使用泛型数据类型实现节点时,我一直收到一个错误。节点必须能够接受整数输入和分数输入。我做错了什么?编译器说“类BinarySearchtree的方法节点(A)未定义Java 实现通用节点,java,binary-tree,nodes,Java,Binary Tree,Nodes,当我尝试使用泛型数据类型实现节点时,我一直收到一个错误。节点必须能够接受整数输入和分数输入。我做错了什么?编译器说“类BinarySearchtree的方法节点(A)未定义 //creates a generic binary search tree class public class BinarySearchTree<A> { //the root of the node, which is the middle value Node root;
//creates a generic binary search tree class
public class BinarySearchTree<A> {
//the root of the node, which is the middle value
Node root;
//this constructor will add a node
public void addNode(A userNumber){
Node<A> newNode = Node<A>(A userNumber);
}//end addNode
public class Node<T>{
//this generic variable will become the user input either int or fraction
private T number;
//nodes that will become the left of right child of a parent node
Node<T> leftChild;
Node<T> rightChild;
//a node constructor that will take a generic input
Node(T number){
this.number = number;
}//end node constructor
}//end the Node class
}//end binary search tree
//创建一个通用的二进制搜索树类
公共类二进制搜索树{
//节点的根,即中间值
节根;
//此构造函数将添加一个节点
public void addNode(用户号){
Node newNode=节点(一个用户编号);
}//结束添加节点
公共类节点{
//此通用变量将成为用户输入的int或fraction
私人T数;
//将成为父节点的左/右子节点的节点
节点左子节点;
节点右子节点;
//将接受一般输入的节点构造函数
节点(T编号){
这个数字=数字;
}//端节点构造函数
}//结束节点类
}//结束二叉搜索树
而不是
Node<A> newNode = Node<A>(A userNumber);
Node newNode=Node(用户编号);
使用
Node newNode=新节点(用户编号);
你没有编译器乐意告诉你的任何方法节点。你能发布错误消息吗?它们非常具有描述性,会告诉你到底是哪里出了问题。你为什么不在节点根中为节点指定t?我怀疑它在Java中是否行,尽管我不记得它是100%。也许你不需要将泛型用作整数你忘了
new
关键字。哦,天哪,我怎么会忘了“new”。这么简单的错误,非常感谢,我对编码还是很陌生
Node<A> newNode = new Node<A>(A userNumber);