File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterate through fields in an Inner Class

 
Dieter Stryker
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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:

 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Keith R Wegner
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic