java程序打印奇怪的十六进制输出
好的,我正在写一个二叉搜索树的程序。一切看起来都很好,除了我的程序一直打印这个,而不是我的整数顺序遍历。我试着在main方法中只打印ln,得到了同样的结果 这是我的代码:java程序打印奇怪的十六进制输出,java,println,Java,Println,好的,我正在写一个二叉搜索树的程序。一切看起来都很好,除了我的程序一直打印这个,而不是我的整数顺序遍历。我试着在main方法中只打印ln,得到了同样的结果 这是我的代码: public class bst { Node root; public Node getRoot(){ return root; } public bst(){ root=null; } //method addNode publi
public class bst {
Node root;
public Node getRoot(){
return root;
}
public bst(){
root=null;
}
//method addNode
public void insert(int key){
Node newNode= new Node(key);//initialize Node
if(root==null){
root=newNode;
}else{
Node focusNode=root;
Node insNode=root;
while(insNode!=null){
focusNode=insNode;
if(key<focusNode.getKey()){
insNode=insNode.getLeft();
}
else{
insNode=insNode.getRight();
}
}
if(key<focusNode.getKey()){
focusNode.setLeft(newNode);
}
else{
focusNode.setRight(newNode);
}
}
}
public void inOrder(Node focusNode){
if (focusNode !=null){
inOrder(focusNode.leftChild);
System.out.println(focusNode);
inOrder(focusNode.rightChild);
}
}
//Node class
class Node{
int key;
Node leftChild;
Node rightChild;
//Node constructor
Node(int key){
this.key=key;
leftChild=null;
rightChild=null;
}
public void setLeft(Node left){
this.leftChild=left;
}
public void setRight(Node right){
this.rightChild=right;
}
public Node getLeft(){return leftChild;}
public Node getRight(){return rightChild;}
public void setKey(int k){this.key=k;}
public int getKey(){return key;}
public void print(){
System.out.println(getKey());
}
}
public static void main(String[] args){
bst theTree= new bst();
theTree.insert(30);
theTree.insert(60);
theTree.insert(50);
theTree.insert(70);
theTree.inOrder(theTree.getRoot());
}
}
公共类bst{
节根;
公共节点getRoot(){
返回根;
}
公共bst(){
root=null;
}
//方法addNode
公共无效插入(int键){
Node newNode=新节点(键);//初始化节点
if(root==null){
根=新节点;
}否则{
节点focusNode=root;
Node=root;
while(insNode!=null){
focusNode=insNode;
如果(在顺序方法中输入,则执行以下操作:
System.out.println(focusNode);
您正在直接打印focusNode
,因此除非您的节点
类重写默认的toString
方法,否则您只会看到对象的哈希代码(如果您感兴趣,请参阅以获取详细信息)。您可能希望
System.out.println(focusNode.getKey());
或者只需使用您编写的print
方法即可。看起来您正在尝试打印实际节点,它将只是该节点的内存地址。如果要打印整数,则应打印节点的密钥
print(node.getKey());