• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested iterations

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class:


I want to know if there is a way to iterate the people list recursively for each Person, instead of writing n-iterator loops since I won't know at runtime how many nested people objects there are in each Person.

Thanks
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for-each loop of JDK5.0 is a wonderful feast to eyes when it comes to iterate over nested collections.
Refer this link for more details.

Hope that helps
[ March 17, 2008: Message edited by: Anubhav Anand ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there are iterations inside Person, that is Person's problem.
If there are such iterations, Person should declare them in its javadoc pages, and tell you how to use them; there has to be a method which does that.
You call whichever method there is, and that should be designed to take care of all iterations. If it doesn't, then Person has been designed incorrectly. In fact having such public fields is bad practice, breaching data encapsulation and hiding. Also having Person objects inside fields of the Person class risks getting into an infinite recursion.

You might be able to get this sort of thing to workGood luck with it
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's one example:


[ March 17, 2008: Message edited by: Daniel Chemko ]
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not bad. Only six levels of search and you get the entire population of the earth!
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rad. Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic