This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes How to print all element of List over console? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to print all element of List over console?" Watch "How to print all element of List over console?" New topic
Author

How to print all element of List over console?

viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

I have a list collection which having some element and i want to check it that which element it has....please reply me.
thanks!
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Vivek, you can simply sysout list object.
viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

thanks! Harsh,
but when i trying to do as you said then it prints className@Hashcode(Maintenance@j1296)
here i have a following code like that..

cList=(List)request.getAttribute("maintObj");

now i want to check the data contains by List cList and wanna see entire data contained by the cList...
how should i do???
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Hey Vivek, you have kept objects of Maintenance class inside list. Please override toString() in Maintenance class and everything should be fine. Hope this is helpful.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38433
    
  23
harshvardhan ojha wrote: . . . Hope this is helpful.
Yes, it is

But I would suggest, maybe, you might possibly like to give a little more explanation and describe the behaviour of toString a bit more.
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

why not Campbell,

We override toString() method from Object class to give meaningful String representation of the Object.
Say your class is



My output is : Maintainance@767946a2

This is class name+'@'+hex version of the object hashcode. This doesn't make any sense

lets say i want to understand more about my object i will override this toString() method to this class



My output will be : Maintainance [name=Harshvardhan, age=25]

And this info makes sense. So its always good to have toString(). Similarly please read on equals() and hashcode() also.
I hope this will be helpful.

Thanks Campbell for figuring it out, i always make assumptions like this.
 
wood burning stoves
 
subject: How to print all element of List over console?