5

How can I deep copy an irregularly shaped 2D array in Java?

Ie.

int[][] nums =  {{5},
                 {9,4},
                 {1,7,8},
                 {8,3,2,10}}

I'm unable to use Arrays.arrayCopy() for some reason (versioning?)

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Sean McDaid
  • 71
  • 1
  • 2
  • 7
  • 1
    There is no Arrays.arrayCopy(). There's System.arrayCopy() (which existed since Java 1.0) and there's Arrays.copyOf() (which exists since Java 6/Java 1.6). Please clarify what you meant. – Joachim Sauer Jan 07 '09 at 10:44
  • Seems to be exact duplicate - see guerda's answer – The Archetypal Paul Jan 07 '09 at 10:47
  • @Paul: This is not a duplicate of that question - did you even read it? – Draemon Jan 07 '09 at 10:49
  • Agreed, it's not an exact duplicate, it's a much more specific question with wildly different answers (answers from the other question can still be helpful, 'though). – Joachim Sauer Jan 07 '09 at 10:56
  • May [this question](http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object) help you? – guerda Jan 07 '09 at 10:42

7 Answers7

13
int[][] copy = new int[nums.length][];

for (int i = 0; i < nums.length; i++) {
    copy[i] = new int[nums[i].length];

    for (int j = 0; j < nums[i].length; j++) {
        copy[i][j] = nums[i][j];
    }
}

You can replace the second loop with System.arraycopy() or Arrays.copyOf().

Joao da Silva
  • 7,353
  • 2
  • 28
  • 24
7

I wrote this in Eclipse, tested it, came back and found that João had beaten me to almost exactly the same solution. I upvoted him, but here's mine for comparison. I guess it's instructive to see the very slight details people choose to do differently.

private static int[][] copy2d(int[][] nums) {
    int[][] copy = new int[nums.length][];

    for (int i = 0; i < copy.length; i++) {
        int[] member = new int[nums[i].length];
        System.arraycopy(nums[i], 0, member, 0, nums[i].length);
        copy[i] = member;
    }

    return copy;
}

For extra credit, try writing one that copies an n-dimensional array where n is arbitrary.

slim
  • 40,215
  • 13
  • 94
  • 127
3

N-dimensional deep copy

public class ArrayTest extends TestCase {

    public void testArrays() {
        Object arr = new int[][]{
                {5},
                {9, 4},
                {1, 7, 8},
                {8, 3, 2, 10}
        };

        Object arrCopy = copyNd(arr);
        int height = Array.getLength(arr);
        for (int r = 0; r < height; r++) {
            Object rowOrigonal = Array.get(arr, r);
            Object rowCopy = Array.get(arrCopy, r);
            int width = Array.getLength(rowOrigonal);
            for (int c = 0; c < width; c++) {
                assertTrue(rowOrigonal.getClass().isArray());
                assertTrue(rowCopy.getClass().isArray());
                assertEquals(Array.get(rowOrigonal, c), Array.get(rowCopy, c));
                System.out.println(Array.get(rowOrigonal, c) + ":" + Array.get(rowCopy, c));
            }
        }
    }

    public static Object copyNd(Object arr) {
        if (arr.getClass().isArray()) {
            int innerArrayLength = Array.getLength(arr);
            Class component = arr.getClass().getComponentType();
            Object newInnerArray = Array.newInstance(component, innerArrayLength);
            //copy each elem of the array
            for (int i = 0; i < innerArrayLength; i++) {
                Object elem = copyNd(Array.get(arr, i));
                Array.set(newInnerArray, i, elem);
            }
            return newInnerArray;
        } else {
            return arr;//cant deep copy an opac object??
        }
    }
}
Chii
  • 14,540
  • 3
  • 37
  • 44
2

Some folks suggest clone() -- just to be extra clear, clone() on a multi-dimensional array is only a shallow clone. original.clone()[0] == original[0]. But (for primitives) you can use clone() instead of System.arraycopy() once you're down to one-dimensional arrays.

David Moles
  • 48,006
  • 27
  • 136
  • 235
1

Here's one that specializes to deeply cloning int[][]. It also allows any of the int[] to be null.

import java.util.*;

public class ArrayDeepCopy {

    static int[][] clone(int[][] arr) {
        final int L = arr.length;
        int[][] clone = new int[L][];
        for (int i = 0; i < clone.length; i++) {
            clone[i] = (arr[i] == null) ? null : arr[i].clone();
        }
        return clone;
    }

    public static void main(String[] args) {
        int[][] a = {
            { 1, },
            { 2, 3, },
            null,
        };
        int[][] b = a.clone();
        System.out.println(a[0] == b[0]); // "true", meaning shallow as expected!

        b = clone(a); // this is deep clone!
        System.out.println(Arrays.deepEquals(a, b)); // "true"
        System.out.println(a[0] == b[0]); // "false", no longer shallow!
    }
}
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
0

Here is a simple convenient way to copy 2 dimensional arrays (compatible with DEEP copy) :

public static char[][] cloneArray(char[][] array){
 char[][] copy = new char[array.length][];
 for(int i = 0 ; i < array.length ; i++){
  System.arraycopy(array[i], 0, copy[i] = new char[array[i].length], 0, array[i].length);
 }
 return copy;
}

plz note that you simple have to change array type to anything else, like int

JeanRe
  • 1
0

Another arbitrary n-d copy. It's ugly, and thanks to Java's type system you can't cast the result back to the array type you started with. Still, it works. Like the other comments say, use clone() :)

public  void testMultiDimArray()
{
   int[][][] arr = new int[][][] {
           { {5}, {5, 6 }, {3, 3, 1} },
           { {1, 2, 3}, {4, 5 } }
   };

   Object[] dest = (Object[]) deepCopy(arr);
   // System.out.println(Arrays.deepToString(dest));
   assertTrue(Arrays.deepEquals(arr, dest));
}

public static Object deepCopy(Object src)
{
    int srcLength = Array.getLength(src);
    Class srcComponentType = src.getClass().getComponentType();

    Object dest = Array.newInstance(srcComponentType, srcLength);

    if (srcComponentType.isArray())
    {
        for (int i = 0; i < Array.getLength(src); i++)
            Array.set(dest, i, deepCopy(Array.get(src, i)));
    }
    else
    {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }

    return dest;
}
Charles Miller
  • 2,867
  • 1
  • 21
  • 14