How to copy elements from array one to array two using recursion?

2.8k views Asked by At

I am trying to copy the elements from src (Array 1) to tgt (Array 2) using recursion. Len is an integer value that determines how many of the elements should be transferred. For example, if len is 4, I take 4 elements out of Array 1 and transfer them to Array 2.

Start is the starting location of array src, and this value is transferred to location start2 of array tgt. Then I recursively copy the remaining len-1 elements. Returning out of bound exception.

public void arraycopy(double[] src, int start, double[] tgt, int start2, int len){
    if(len < 1) return;
    if(len > src.length) return;

    tgt[start2] = src[start];
    arraycopy(src, start + 1, tgt, start2 + 1, len);

}
4

There are 4 answers

1
rgettman On

First, you are not treating len as the number of characters to copy. You are treating it as an ending index with this condition:

if(start < len){

You can change it to

if (len > 0)

but at that point, len is already greater than 0 because the base case len < 1 is already past. You can remove that if condition entirely.

Second, pass len - 1 in the recursive call:

arraycopy(src, start+1, tgt, start2+1, len - 1);

Third, if len is greater than the source array's length:

if (len > src.length) return;

then all you do is return, leading to an uncopied array and a confused caller. I would remove this line entirely. You can let Java throw the ArrayIndexOutOfBoundsException, passing it to the caller. If you must perform the bounds check, then test it yourself properly, for both the source and destination arrays.

if (start < 0 || start + len > src.length || start2 < 0 || start2 + len > tgt.length) {
    throw new IllegalArgumentException("Out of bounds");
}
0
Seeni On

You don't have to pass two position integer. One is enough. Check out the code. And also pass the actual length not (len-1)

public static void arraycopy(double[] src, int start, double[] tgt, int len){
   if (len != src.length) return;
   if (start<len){
         tgt[start] = src[start];
         arraycopy(src, start+1, tgt, len);
   }
} 
0
B Cronyn On

I have to say that my first attempt had some definite confusion mixed in. Thanks to rgettman^ I was able to make my code more concise and have it pass all my tests!

public void arraycopy(double[] src, int start, double[] tgt, int start2, int len){
   if (start < 0 || start + len > src.length || start2 < 0 || start2 + len > tgt.length) return;
   if (len > 0){
     tgt[start2] = src[start];
     arraycopy(src, start+1, tgt, start2+1, len-1);
   } 
} 
1
satish hiremath On

public class RecursiveArrayCopyAlgorithm {

public static void main(String[] args) {

    String str = "ABCD";

    char[] chars =  str.toCharArray();

    char[] charElements = new char[chars.length];

    int charLen = chars.length-1;

    int charInitialIndex = 0;

    RecursiveArrayCopyAlgorithm testSearch = new RecursiveArrayCopyAlgorithm();

    char[] charObjs = testSearch.callLinear(charLen, charInitialIndex, chars, charElements);

    for (int i = 0; i < charObjs.length; i++) {         
        System.out.println(charObjs[i]);
    }
}

private char[] callLinear(int charLen, int index,char[] ch, char[] charElements) {
    int index1 = charLen;
    if(index1 < 0) {
        return charElements;
    }

    if(index1 >= 0) {
        charElements[index] = ch[index1];
    }

    return callLinear(charLen-1, index+1, ch, charElements);
}

}