File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes print a HashSet of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "print a HashSet of objects" Watch "print a HashSet of objects" New topic
Author

print a HashSet of objects

Gil Shoam
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Hi,
sorry if this should be in Java for beginners. I wasn't sure..

Anyway, I'm writing a program that deals with graphs of any primitive types.
I have 3 classes.
One represents the Nodes:

Another the Edges


And finally the Graph:


Just to test it I ran this Main:



Problem is as you can see, it prints the object address.
I actually need to write a String representation of the graph inc the nodes and edges.
i.e the output should be:
Nodes: {A,,B,C,D}
Edges: {(A,B),(A,C),(B,C)


(here I tried printing only the nodes to see if it works, but no luck)
Since the actual node is an object how can I downcast it to the type I initialized it with?
Or in general- How do I override object toString() method for the graph class?

Thanks in advance

Gil
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch Gil

So do you want to see Node's instance variable _object ? then override Object's toString method in Node class
Gil Shoam
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.
i.e the output for the main (above) should be:
Nodes: {A,,B,C,D}
Edges: {(A,B),(A,C),(B,C)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gil Shoam wrote:Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.


Then your Graph class needs to override toString() to pring out the nodes and edges how you want. And if you have classes for Node and/or Edge, they need to override toString to display themselves appropriately.
Gil Shoam
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Jeff Verdegan wrote:
Gil Shoam wrote:Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.


Then your Graph class needs to override toString() to pring out the nodes and edges how you want. And if you have classes for Node and/or Edge, they need to override toString to display themselves appropriately.


That I know..

How do I do it? Since I don't know In advance what types the graph has.
i tried overriding the toString() in the Node Class using the StringBuffer but it does not make sense...
if I don't yet know what type it is how can I write a string method for it?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gil Shoam wrote:
Jeff Verdegan wrote:
Gil Shoam wrote:Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.


Then your Graph class needs to override toString() to pring out the nodes and edges how you want. And if you have classes for Node and/or Edge, they need to override toString to display themselves appropriately.


That I know..

How do I do it?


That's a really broad question. You should be able to find tons of examples of overriding toString() with a simple web search. What did you try and where exactly did you get stuck?

Since I don't know In advance what types the graph has.


You don't need to. Graph's toString() just says something like:

except, of course, you'd do that in a loop that walks over the nodes.

The individual nodes are then responsible for their own toString(). If a node is just any old class, then it's up to the author of that class to provide a reasonable toString(). If, on the other hand, you have a Node class that contains data and pointers to other Nodes, then you could have its toString() simply return the data's toString().

So you could have Graph's toString() which calls Node's toString() which calls it's data's toString(), with each layer adding whatever structural stuff it knows about/is responsible for (commas, brackets, etc.).

i tried overriding the toString() in the Node Class using the StringBuffer but it does not make sense...


Not sure why you'd use SB. And without seeing what you tried and what problems it caused, I can't really tell you where you went wrong.

if I don't yet know what type it is how can I write a string method for it?


You don't. And you don't need to. It provides its own.
Gil Shoam
Greenhorn

Joined: Dec 21, 2011
Posts: 13
So you could have Graph's toString() which calls Node's toString() which calls it's data's toString(), with each layer adding whatever structural stuff it knows about/is responsible for (commas, brackets, etc.).


I tired this in the node class and the output I got is the object address not the actual char:


I also tried wrapping it with the Character class but still the same problem...

Any suggestions?
Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gil Shoam wrote:I tired this in the node class and the output I got is the object address not the actual char:


Because you're not storing the Character you pass in.



This says, "Totally ignore the obj argument that was passed to me, and instead create a new Object, and store that."

What you're getting it Object's hashCode, because you have stored an Object that you just created, not a Character that you passed in.
Gil Shoam
Greenhorn

Joined: Dec 21, 2011
Posts: 13
I got it!

In the constructor I didn't use the obj I got. This solved it



Thanks for your help.

If I have more Questions related to this project should I keep posting on this thread or open a new one?

Thanks again,

Gil
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



The first line is totally pointless. It creates a new object, and stores a reference to that object in the _object variable. Then, on the next line, you copy the reference value from the obj parameter into the _object variable, and that new Object that you created and never used becomes eligible for GC.

Just get rid of the first line.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gil Shoam wrote:ore Questions related to this project should I keep posting on this thread or open a new one?



New one, unless it's further clarification on a point that's already come up in this discussion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print a HashSet of objects
 
Similar Threads
HashMap, Iteraring problem
How to check whether two trees have identical structure ?
abstract class constructors
Undirected graph help - using ArrayLists and Maps.
Discussion on recursion, stack overflows, & compiler optimization for tail recurssion