wood burning stoves 2.0*
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
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: 38493
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to print all element of List over console?