• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Iterate through fields in an Inner Class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a TreeMap which contains Team objects.

Inside each Team instance is an ArrayList<Game> - Game is an inner class of Team

How do I iterate through the data structure so I can access the fields in each Game in the ArrayList of a Team

I don't know how to code this.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm not sure if this works or not. You know how to iterator through a map right? Ultimately once you get the Team object, you need an iterator inside the Team object to iterate throught the Game object.

Now the problem is what type of object is Team? If it is some kind of collection then it's easy. If not, is making it into a collection a possible solution?

Another approach is instead of using ArrayList<Game> you use your own collection that extends ArrayList. Then in it you call your iterator in your custom collection to iterate Game.

Hope this helps.
 
Dieter Stryker
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Hi there, I'm not sure if this works or not. You know how to iterator through a map right? Ultimately once you get the Team object, you need an iterator inside the Team object to iterate throught the Game object.

Now the problem is what type of object is Team? If it is some kind of collection then it's easy. If not, is making it into a collection a possible solution?

Another approach is instead of using ArrayList<Game> you use your own collection that extends ArrayList. Then in it you call your iterator in your custom collection to iterate Game.

Hope this helps.


Does this look correct:


EDIT -- My Team class is:

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dieter, you'll need to make the Game class in Team class as public. Secondly, you don't need the Game object inside the Team class i.e. this statement

19. Game game = null;

And you'll need to encapsulate the games list i.e. generate a getGames method in your Team class that will return the games ArrayList. Then instead of this statement



You'll have to write




And remember never to use this kind of syntax



That way a new iterator will be created everytime so the loop will continue iterating over the first element infinitely...
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on your current approach, Ankit is right - your Game class can be public. In fact Team and Game are 2 different things so why not Game public.

Now if Game is public there shouldn't be any problem because you are not trying to access some inner class thing.

I also noticed your Game class is immutable (eg not set methods). Nice.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Based on your current approach, Ankit is right - your Game class can be public. In fact Team and Game are 2 different things so why not Game public.

Now if Game is public there shouldn't be any problem because you are not trying to access some inner class thing.

I also noticed your Game class is immutable (eg not set methods). Nice.



Thanks to both of you.

I was up pretty late trying to code this and was probably suffering from sleep deprivation more than anything. I've made the appropriate changes and am moving forward nicely.

Thanks for the feedback.
 
You can't expect to wield supreme executive power just because
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic