3

Here is a method that is supposed to simply create a new ArrayList copying all the elements of parameter ArrayList arrlist, which I think I've done correctly.

public ArrayList<T> copy (ArrayList<T> arrlist) {
        ArrayList<T> um=new ArrayList<T>();
        for (int i=0;i<arrlist.size();i++)
            um.add(arrlist.get(i));
        return um;

However, I'd like to write this exact same method using recursion only with no loops. Here is what I wrote. The copy method uses a recursive helper method.

public ArrayList<T> copy(ArrayList<T> arrlist) {
    return 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));
    return copy(list,n+1); 
}

Except this does not work. Any suggestions or hints?

8
  • What is error there? Commented Oct 21, 2014 at 20:50
  • "doesn't work" in what way? Describe the specific problem you're asking for help with Commented Oct 21, 2014 at 20:50
  • 1
    Is there a base case to this recursion? Or does it just call itself forever? Commented Oct 21, 2014 at 20:50
  • Just about to say the same thing. No base case, can't see this ever terminating. Commented Oct 21, 2014 at 20:50
  • possible duplicate of iterating through arraylists with recursion Commented Oct 21, 2014 at 20:51

5 Answers 5

3

The problem is that you are creating a new ArrayList in every recursion. And you are not returning it anywhere.

What you should do is, in the helper method, also pass the target ArrayList as a parameter, and add to it rather than a new one.

(And of course, don't forget to return when you're done).

Sign up to request clarification or add additional context in comments.

Comments

1

You are allocating (ArrayList um=new ArrayList();) array list in every recursive call. Also I can not see when the recursive function will stop to call itself

Comments

0

Try this :

private void copy(ArrayList<T> src, ArrayList<T> src_copy, int n) {
  if (n>=0){
    src_copy.add(src.get(n));
    copy(src,src_copy, n-1); 
   }
 }

Then, use it as below :

ArrayList<T> src = ... ; //your arrayList
ArrayList<T> src_copy = new ArrayList<T>();
if (src != null && src.size() > 0) {
   copy(src, src_copy, src.size()-1);
 }

Comments

0

I don't know what error you are getting. but I think this should work.

public ArrayList<T> copy(ArrayList<T> arrlist) {
    ArrayList<T> um = new ArrayList<T>();
    return copy(arrlist, um, 0); 
}

private ArrayList<T> copy(ArrayList<T> arrlist, ArrayList<T> um, int n) {
    if (n < arrlist.size())
        um.add(arrlist.get(n));
    else
        return um;
    return copy(arrlist, um, n+1); 
}

Comments

-1

See the below basic recursive fibonacci function:

public int fib(int n) {
    if(n <= 1) {
        return n;
    } else {
        return fib(n - 1) + fib(n - 2);
    }
}

This works as a recursive function because it has a base case, a path by which the function will return a result that is not another call to itself. You'll need a base case to complete the recursive call when a condition is met, and return the results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.