This content has been marked as final. Show 1 reply
You have an arbritrarily complex hierarchial collection whith a unknown structure.
You wish to apply a method to all references in the tree.
There are only two basic storage mechanisms in java; reference or array.
Setup: Keep a hashtable/dictionary of each visited reference to avoid endless recursion.
Method, called Doit() which takes an Object 'n'
- If n is null return
- if n is already in hashtable return
- Add to hashtable
- if n is of type base class then call target method (might want to decide about thrown exception.)
- reflect through n for members which are either Object or array (base type Object)
- If single member is Object then call Doit
- If array recurse through each item and call Doit()