Java class isntance (overriding the ToString method): " adt.BinaryNode@1e24e45 "

Hi, I redefined the toString() method in my own class, but somehow the result is not quite the one I wanted. Sorry for the newbie question, but I can't figure out where the problem is, any advice / hint is mostly appreciated. Thanks.

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); } } 

DS:

 private void preorder(BinaryNode <type> a){ if (a != null){ System.out.println(a.toString()); preorder(a.left); preorder(a.right ); } } 

applications:

 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(); 

output:

 adt.BinaryNode@1e5e2c3 adt.BinaryNode@18a992f adt.BinaryNode@4f1d0d 

EDIT: fix after prompt "chaitanya10" for msitake

DS:

 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 ); } } 
+4
source share
2 answers

your method accepts BinaryNode<type> as argument , you call toString on brinaryNode<type> not COuntry . you have overriden toString() in COuntry not BinaryTree . change it to

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

OR override toString() in BinaryNode .

+3
source

You call the toString method for BinaryNode, not for the country

+2
source

Source: https://habr.com/ru/post/1443119/


All Articles