This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Recursive Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursive Methods" Watch "Recursive Methods" New topic
Author

Recursive Methods

Henrique Ordine
Ranch Hand

Joined: Sep 03, 2004
Posts: 127
hello everyone,
I wrote this dummy tree class which is associated to itself, and i populate it using a recursive method. The recursive method, getMyChildren, is used to add child objects to parent object, and then grandchildren to children, and so on.
In my method, after the recursion, I test the collection of children and they're always empty, even though they're not.
Does anyone know why this happens? I would appreciate any thoughts.

Here is my code:



J2EE Architect/Developer
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I'm having an extremely hard time following the logic here, or why a method called "getMyChildren" would be the way to build a a tree in the first place. But in any case, there's only one place in the whole program where you call children.add():



So if there are already some children, add a new child. But of course, since we start out with no children, that's never going to change.


[Jess in Action][AskingGoodQuestions]
 
Consider Paul's rocket mass heater.
 
subject: Recursive Methods
 
Similar Threads
Form a tree parent -childs from String
StackOverflowError in hashCode due to recursion
Using java.util.HashSet for Custom Class
what changes should i made in save method in one-to-many relationships
Hibernate ClassCastException