• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return an array containt all the object name of a class...  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retreive them?

Thanks!
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd need to keep track of them as they are created. You can create a List in the Country class and keep track of each object as it gets created.

Note this assumes once an object is created it never goes out of scope. A fair assumption for countries. It also assumes you don't have duplicate country objects. If you do, you can create a factory method rather than using the constructor so you don't create duplicate objects. Or you can use a Set instead of a List so you don't store duplicates.
 
Francis Pru
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank you !!
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!