Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print all element of List over console?

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek, you can simply sysout list object.
 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic