Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have tried everything I can think of and have searched for it everywhere online, but I can't get it to work. Below I have two Java methods, the first one I successfully got to work, and the other I can't make work. The first method, printArray(), accepts any array, regardless of object type and number of dimensions, and prints the results to the screen. The method does this recursively, meaning it calls itself within itself and loops through each dimension that way.

For the second method, deepClone(), I need it to accept any array as well, and return a deep copy. The way it's set up, I can make a clone of the leftmost dimension, but not of other dimensions. Every dimension within the cloned array needs to reference its own place in memory. Wherever I put the recursion (the deepClone() method call), the function seems to either not change in the way it acts, or give a StackOverflowError. I have also tried putting the recursion in the set method and the return statement.

Any help is greatly appreciated. I have to do this using reflection, and the method has to be static. The recursion is optional. Any proposed way of achieving this, either recursively or through another way, is greatly appreciated.

public static void printArray(Object array) { // This method works fine
    if (array.getClass().isArray())
        for (int i = 0; i < Array.getLength(array); i++) {
            printArray(Array.get(array, i));
            System.out.println();
        }
    else
        System.out.print("{" + array + "} ");
}

public static Object deepClone(Object array) { // This method does not work properly
    Class c = array.getClass().getComponentType();
    Object newArray = new Object();
    if (c.isArray()) {
        newArray = Array.newInstance(c, Array.getLength(array));
        for (int i = 0; i < Array.getLength(newArray); i++) {
            deepClone(Array.get(array, i));
            Array.set(newArray, i, Array.get(array, i));
        }
    }
    return newArray;
}

Thank you.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You have a number of problems.

(1) You are just making the clone and throwing it away, instead of returning it from the deepClone method.

(2) You're not copying the atoms from the old array to the new array - you're just replacing them with uninitialised Object objects.

(3) You're checking whether the thing that you're copying is an array of arrays, where you should just check whether it's an array.

Here is my version. Note that the two arrays share the same atoms, but the arrays themselves are cloned.

public static Object deepClone(Object toClone) { 
    Class<?> c = toClone.getClass();
    if (c.isArray()) {
        Object toReturn = Array.newInstance(c.getComponentType(), Array.getLength(toClone));
        for (int i = 0; i < Array.getLength(toReturn); i++) {
            Array.set(toReturn, i, deepClone(Array.get(toClone, i)));
        }
        return toReturn;
    }
    else {
        // toClone is an atom, so just return it
        return toClone;
    }
}
share|improve this answer
    
Sorry, but that doesn't work. I get this in the terminal: Exception in thread "main" java.lang.IllegalArgumentException: array element type mismatch –  user1567060 Nov 17 '13 at 4:43
    
Do I need to cast it? –  user1567060 Nov 17 '13 at 4:45
    
I don't think so. Hang on, I'll do some testing and fix this up. –  David Wallace Nov 17 '13 at 5:20
    
OK, it works now! I didn't notice the other two issues at first. –  David Wallace Nov 17 '13 at 5:39
    
Your answer worked perfectly. Thank you very much! I am sure this method will come in handy for many more projects. I appreciate you thinking it through with me and offering your insight. –  user1567060 Nov 17 '13 at 21:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.