The moose likes Beginning Java and the fly likes Linked List need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linked List need help" Watch "Linked List need help" New topic
Author

Linked List need help

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 148


In the program above i have two doubts

A. What is the use of toString() method in Address class ??
B. Why can't i directly put object m1 in print statement in the MailList class ?? The program is not compiling by doing that, error is not able to identify variable m1.

Somebody please reply
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The toString() method is called when you intend to print an object. Since you have overridden the toString() in the Address class it prints nicely the name, street, city.. etc with line breaks.

This method will be called when you print the Address object directly or the List where you have added one or many Address objects.

But you have to provide the List reference correctly I fear your Address List is named ml and you try to print m1

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 148
Thanks for the reply John, ya its was my fault putting ml as m1.

i have one more doubt earlier for an instance in the program given below there was no need of overriding toString() method
for displaying linked list. Directly the linked list object is given in the print statement. But for the prior code without overriding
toString() method,

"[Address@108c21a, Address@1ff6599, Address@676175]"

this is the output. Its not working. As far as i think the prior program should show the correct output without overriding
the toString() method.




John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Without overriding the toString() method in the Address class, the toString() method in the Object class gets called.

Read here what prints when the toString() method in Object class is called - http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString()

Without overriding the toString() method, you cannot make your custom output (the address representation) printed.

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 148
but John how come the output is fine in this code without overriding the toString() method, sorry for asking so many questions but i need to understand the concept



This is the output which i am getting


Original contents of ll: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
shivang sarawagi wrote:without overriding the toString() method,


The Address class printed nice output since it overrides the toString() method. So check what is the class the List is holding... A String class?? So to make the String class print its content nicely what should have been done?? Any guess ?

sorry for asking so many questions but i need to understand the concept

You are always welcome to ask questions here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
shivang sarawagi wrote: . . . without overriding
toString() method,

"[Address@108c21a, Address@1ff6599, Address@676175]"

this is the output. Its not working. . . .
Read the details of Object#toString() and you find that is normal behaviour. There is nothing wrong with it; only, as the link will tell you, you ought to override toString the way you want it to work, in all your classes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
shivang sarawagi wrote: . . . the output is fine in this code without overriding the toString() method, . . .
You are adding Strings. The String class does override the toString() method already. It is probably very simple, like this:
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 148
I got it. Thanks John & Campbell for your prompt replies
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked List need help