aspose file tools*
The moose likes Beginning Java and the fly likes Collection and tostring() method overridden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collection and tostring() method overridden " Watch "Collection and tostring() method overridden " New topic
Author

Collection and tostring() method overridden

C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
Hi,

Please help me in knowing this concept.

import java.util.*;

public class Dvdinfo implements Comparable<Dvdinfo>
{
String title;
String genre;
String leadActor;

Dvdinfo(String t,String g,String k)
{
this.title = t;
this.genre = g;
this.leadActor = k;
}

public static void main(String[] args)
{
Dvdinfo dvi1= new Dvdinfo("sholay","male","Amitabh");
Dvdinfo dvi2= new Dvdinfo("Charas","male","jimmy");
Dvdinfo dvi3= new Dvdinfo("Lagaan","male","Aamir");

System.out.println(dvi1);
System.out.println(dvi2);
System.out.println(dvi3);


ArrayList<Dvdinfo> ard = new ArrayList<Dvdinfo>();
ard.add(dvi1);
ard.add(dvi2);
ard.add(dvi3);



System.out.println("Collections :"+ard);/*********This method calls to string but how come Collections is printed after the message in the tostring method*/

}

public String toString()
{
System.out.println("Inside the method tostring");
return title + " " + genre + " " + leadActor +"\n";
}

public int compareTo(Dvdinfo d)
{

return 5;

}

public String getTitle()
{
return title;

}
}



Question:
In the red coloured line.

Output is as follows:

Inside the method tostring///////////How come is this message getting printed before Collections
Inside the method tostring
Inside the method tostring

Collections :[sholay male Amitabh
, Charas male jimmy
, Lagaan male Aamir
]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to JavaRanch Please use the code button and maintain indentation; your code is difficult to read.

You are calling toString when you print your Dvdinfo objects before you print out the Collection. That is why you are getting that particular result.

By the way: You are not implementing compareTo correctly.
C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
Hi Riitchie,

I have modified the code and removed the printout statements for objects.


The modified code is:




Still the out is as follows:
[Output] Inside the method tostring
Inside the method tostring
Inside the method tostring
Collections :[sholay male Amitabh
, Charas male jimmy
, Lagaan male Aamir
]

[/Output]



Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi Kushtawar,
Welcome to Java Ranch!!!
C Kushtawar wrote:I have modified the code and removed the printout statements for objects.
The modified code is: ............

You could have edited your previous message in the post, as that could have been better.
There's an EDIT button in every message you write.
Now, everyone who has to answer your question by seeing your code here and search for your questions in the previous message....... :roll:
I hope we don't have to do that because I guess you have got the answer, CR has already answered it.......




SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
The toString method is called by the toString method of the Collection. Go and find the ArrayList class and its toString method.
What happens is that the toString method of each of the elements is called (printing "inside toString"), then the result is put together with the other results, to create the output String, then the output String is returned to your println() method. So you get "inside toString" printed first.
C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
I will take care of that.

Thanks to both of you.
Just wanted to know does this rule apply only to collections or java as a whole.

I must accept I expected Collections to get printed at the top of the output .

Thanks,
Chandan
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
C Kushtawar wrote:Just wanted to know does this rule apply only to collections or java as a whole.

Even the System.out.print() method calls the toString() method if you send it an object.


C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
It may sound silly on my part but still:

When the ArrayList class calls its toString method ,even in that case Collections should have got printed followed by tostring println statements as in:

Collections :
Inside the method tostring
Inside the method tostring
Inside the method tostring
[sholay male Amitabh
, Charas male jimmy
, Lagaan male Aamir
]

But this does not happen.I mean first we print Collection and then everything should follow.
Ideally when we print something as below:
int i=80;
System.out.println("Collections "+i);

The output is:
Collections 80

But same rule does not apply to our Arraylist and its related tostring methods.

I am still not convinced why does this happen.





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I have already explained why the toString method prints out first.

Get a pencil and paper and write down all the stages in each method. Remember the Collections.toString method cannot do anything until all its elements have completed their toString methods.

If you can't understand it, maybe somebody else can explain better than me.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
C Kushtawar wrote:When the ArrayList class calls its toString method ,even in that case Collections should have got printed followed by tostring println statements

Did you actually see the toString() method in ArrayList, the link that CR gave you?
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
It seems the toString of the collection iterates through its elements and saves up the return value of their toString() in one big String object.
Every time it calls an element's toString(), the function prints "Inside the method tostring". After calling toString() for all elements, meaning
a number of "Inside the method tostring" strings are printed equal to the number of elements in the collection, the total of return values
is printed and gives the remaining info.

Something like:

declare bigstring
loop through all elements
call tostring, and add the returned value to bigstring -> this call also writes "Inside the method tostring"
continue till end of collection
print bigstring

Hope it helps!

Java hobbyist.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sachin Adat wrote:You could have edited your previous message in the post, as that could have been better.
Editing is a possibility, but in this sort of instance it is probably better to see the original code and the changes, so I think C Kushtawar was correct posting like that.
C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
Thanks thats what I wanted to know....the lists are treated differently.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection and tostring() method overridden