• Post Reply Bookmark Topic Watch Topic
  • New Topic

My arrayList stole my objects and won't give them back!  RSS feed

 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - that's a bit dramatic...

I've got a whole bunch of objects with stuff in common, and I don't know how many there will be, so I thought an arraylist would be a handy way to organize them. I have loaded it like so:

The problem is -- I don't know how to retrieve all of my data from my objects. I've looked around and found people who have suggested something like the following:

This outputs "GoogleDir.chsPolygon@6b62d1" - which I don't quite understand. If you are so kind as to point me in the right direction, what I'd love is something I could throw into a loop that would allow me to pull out all of the info from all of my objects.

Thanks ever-helpful friends,
Mike
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This outputs "GoogleDir.chsPolygon@6b62d1" - which I don't quite understand.

All Classes automatically inherit from Object.
Since you've not overriden the Object#toString() method, it uses that when you pass an instance of myPolygon to System.out.println( .. ).

In summary, implement myPolygon#toString to print out all the points and name.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:

All Classes automatically inherit from Object.
Since you've not overriden the Object#toString() method, it uses that when you pass an instance of myPolygon to System.out.println( .. ).

In summary, implement myPolygon#toString to print out all the points and name.


I don't think I understand - I've added "out.println(p_charts.toString());" (where p_charts is the name of my arraylist), and my output is "[GoogleDir.chsPolygon@1bbd3e2, GoogleDir.chsPolygon@53abbb]"

I really appreciate your help - but could you clarify?
[ January 08, 2008: Message edited by: Mike McMahon ]
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This statement will execute roughly like this:


Since your myPolygon does not have a custom toString() method, it will
call Object.toString(). Please RTFM specifically javadoc for class Object.
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when you make these statements this will get an object of cshPolygon

now to get instance fields of your objects you have to read that fields explicitly

like


hope this way you can get the wanted results.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Vijay -
Thanks - that's helpful for most aspects of my objects arrayList.

Pho was helping me with the more troublesome part where I've got an object loaded into the arraylist that I apparently need to RTM to learn how to override the toString() method to get my values back out of it.

Thanks for your help!
Mike
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

Now if you have solved your problem you better write a nice toString() method for every class you write. It will better help you to understand your Objects at runtime.

vijay
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!