aspose file tools*
The moose likes Beginning Java and the fly likes Iterate through fields in an Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterate through fields in an Inner Class " Watch "Iterate through fields in an Inner Class " New topic
Author

Iterate through fields in an Inner Class

Dieter Stryker
Greenhorn

Joined: Mar 21, 2009
Posts: 11
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.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Dieter Stryker
Greenhorn

Joined: Mar 21, 2009
Posts: 11
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:

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

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.
Keith R Wegner
Greenhorn

Joined: Mar 17, 2009
Posts: 8
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.


keith.wegner@gmail.com
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Iterate through fields in an Inner Class