Saturday, 15 February 2014

list - adding elements of another arraylist by recursion-java -



list - adding elements of another arraylist by recursion-java -

here method supposed create new arraylist copying elements of parameter arraylist arrlist, think i've done correctly.

public arraylist<t> re-create (arraylist<t> arrlist) { arraylist<t> um=new arraylist<t>(); (int i=0;i<arrlist.size();i++) um.add(arrlist.get(i)); homecoming um;

however, i'd write exact same method using recursion no loops. here wrote. re-create method uses recursive helper method.

public arraylist<t> copy(arraylist<t> arrlist) { homecoming copy(arrlist,0); } private arraylist<t> copy(arraylist<t> arrlist, int n) { arraylist<t> um=new arraylist<t>(); if (n<arrlist.size()) um.add(list.get(n)); homecoming copy(list,n+1); }

except not work. suggestions or hints?

the problem creating new arraylist in every recursion. , not returning anywhere.

what should is, in helper method, pass target arraylist parameter, , add together rather new one.

(and of course, don't forget homecoming when you're done).

java list recursion arraylist

1 comment: