您好,我toString()
在自己的类中覆盖了方法,但不知何故,输出并不完全是我想要的。对不起新手问题,但我无法弄清楚问题出在哪里,任何提示/帮助都非常感谢。谢谢你。
我的课:
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);
}
}
数据:
private void preorder(BinaryNode <type> a){
if (a != null){
System.out.println(a.toString());
preorder(a.left);
preorder(a.right );
}
}
应用程序:
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
编辑:在 msitake 的“chaitanya10”提示之后修复
数据:
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 );
}
}