File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursive Methods" Watch "Recursive Methods" New topic

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

Joined: Jul 08, 2003
Posts: 24195

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]
It is sorta covered in the JavaRanch Style Guide.
subject: Recursive Methods
It's not a secret anymore!