recursion - How to access recursively to end nested objects in java -
i want access other nested objects recursively using lastly object "mul1" because contains other objects. returns 1 object. not vector object. please 1 can give solution access these nested objects.
formulaelement varx=new variableelement("x"); formulaelement c7=new constantelement(7); functionelement plus=new plusfunctionelement(); plus.addargument(varx); plus.addargument(c7); formulaelement c2=new constantelement(2); functionelement min=new minusfunctionelement(); min.addargument(varx); min.addargument(c2); functionelement mul1=new multiplefunctionelement(); mul1.addargument(plus); mul1.addargument(min);
edit:
formula element not contains method yet. want add together recursive method find nested elements in formulaelement class. functionelement class :
public class functionelement extends formulaelement { private vector<formulaelement> arguments; public functionelement() { arguments = new vector<formulaelement>(); } public void addarg(formulaelement element) { arguments.add(element); } public void setargs(vector<formulaelement> arguments) { this.arguments = arguments; } public vector<formulaelement> getargs() { homecoming arguments; } public formulaelement getarg(int i) { homecoming arguments.get(i); } public string tostring() { homecoming this.tostring(); } }
keep track of caller , passed argument
well not best solution can utilize arraylist
( or else follow idea ) maintain track of caller
, argument
.
for example can alter addargument
method addargument(caller,argument);
, after within argument
can add together both object class level variable arraylist<object[]>
.
but duty check whether there should not duplication in arraylist
.
you can create separate class same.
java recursion
No comments:
Post a Comment