Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java类isntance(ToString方法重写)输出;adt。BinaryNode@1e24e45&引用;_Java_String_Instance - Fatal编程技术网

Java类isntance(ToString方法重写)输出;adt。BinaryNode@1e24e45&引用;

Java类isntance(ToString方法重写)输出;adt。BinaryNode@1e24e45&引用;,java,string,instance,Java,String,Instance,您好,我已经在自己的类中重写了toString()方法,但不知何故,输出并不完全是我想要的。很抱歉我的问题是新手,但我不知道问题出在哪里,任何提示/帮助都非常感谢。多谢各位 Myclass: public class Country implements Comparable<Country>{ private String name; private String capital; private int area; public Country(

您好,我已经在自己的类中重写了
toString()
方法,但不知何故,输出并不完全是我想要的。很抱歉我的问题是新手,但我不知道问题出在哪里,任何提示/帮助都非常感谢。多谢各位

Myclass:

public class Country implements Comparable<Country>{
    private String name;
    private String capital;
    private int area;

    public Country(String a, String b, int c) {
        this.name = a;
        this.capital = b;
        this.area =c;
    }

    @Override
    public String toString(){
        return(this.name + " "+ this.capital+" " + this.area);
    }
}
编辑:在msitake的“chaitanya10”提示后修复

DS:

private void预订单(二进制节点a){
如果(a!=null){
System.out.println(a.elm.toString());//访问节点中的数据,而不是孔节点。
前序(a.左);
预订单(a.右);
}
} 

您正在调用BinaryNode的toString方法,而不是Country

您的方法将
BinaryNode
作为
参数
,您正在
brinaryNode
上调用
toString
。您已在
国家/地区
中覆盖了
而不是
二进制树
。
换成

private void  preorder(Country a){
      if (a != null){
       System.out.println(a.toString());

      }
} 

重写BinaryNode中的toString()

预排序
是一种
BinarySearchTree
方法,它不能专门针对
国家实施。。。还是我遗漏了什么?@JuanMendes tbh,我不熟悉二进制树方法,我只是在指出他的错误:)@Bogdan。谢谢:)我希望您正在重写BinaryNode中的toString():)BinarySearchTree的第三方库是什么?这就是问题所在。noop已修复,需要
nod.element.toString
BinarySearchTree <Country> db = new BinarySearchTree<Country>();
    Country ob  = new Country("Romania", "Buc", 123);
    db.addNewElement(ob);
        ob  = new Country("Hungaria", "Bud", 50);
    db.addNewElement(ob);
        ob  = new Country("Vatican", "Vat", 1);
    db.addNewElement(ob);
    db.printAll();
adt.BinaryNode@1e5e2c3
adt.BinaryNode@18a992f
adt.BinaryNode@4f1d0d
private void  preorder(BinaryNode  <type>  a){
      if (a != null){
       System.out.println(a.elm.toString()); // ACCES the data in node not the hole node.
       preorder(a.left);
       preorder(a.right );
      }
} 
private void  preorder(Country a){
      if (a != null){
       System.out.println(a.toString());

      }
}