0

I am trying to create a deep copy of a two-dimensional array of types int, double, boolean.

This question Write a generic method to copy an array helped me quite a bit (see below). But the problem is that actually in my code it would be a lot better to be able to simply deepCopy an array of primitive types.

private <T> T[][] arrayCopy(T[][] original) {
    Class<?> arrayType = original.getClass().getComponentType().getComponentType();

    int[] dims = {original.length, original[0].length};
    T[][] copy = (T[][]) java.lang.reflect.Array.newInstance(arrayType, dims);

    for(int i = 0; i<dims[0]; i++){
        for(int j = 0; j<dims[1], j++){
            copy[i][j] = original[i][j];
        }
    }
    return copy; 
}

I have very limited knowledge about Java generics and I am unsure if what I wish is possible. I would greatly appreciate any help.

  • Since Java 5, all premitive data type has corresponding object type. it is totally fine for you to go with the way you shows. What's your problem? – Qingfei Yuan Jun 17 '19 at 14:19
  • 1
    If you just want a deep copy, calling clone() is sufficient on 2d arrays of ints, doubles etc. – Satyendra Kumar Jun 17 '19 at 14:28
  • You can't use generics with primitive types. The `T` (or whatever) generic type variable is only compatible with Object types (classes, interfaces, enums). – RealSkeptic Jun 17 '19 at 14:28
  • 1
    @SatyendraKumar How so? That's not a deep copy. Each element in the array points to the same element as the old array. Change one of the ints in one, and it changes in the other. – RealSkeptic Jun 17 '19 at 14:29
  • Also note that your method is incorrect. It assumes the array is rectangular (that all elements have the same size). That's not generally correct for Java arrays. You can have a `T[][]` with three elements in the first row, none in the second row, and 600 in the third row, etc. – RealSkeptic Jun 17 '19 at 14:40
  • @QingfeiYuan when I use an int[][] as argument, the program does not compile, saying "method... cannot be applied to given types, required T[][], given int[][]". So I guess RealSkeptic is right, that generics cannot be used with primitive types... – ProgrammingBeginner Jun 17 '19 at 14:55

1 Answers1

0

Generics doesn't work for primitive types. Arrays of primitives are reference types and you could use generics to match the input and output types, but you can't apply a bound to check that it's an array type because the only supertype of primitive array types is Object. To have the same code work on all array types, you would have to use the special methods in java.lang.reflect.Array to get and set array elements, get length, etc.

import java.lang.reflect.Array;

private <T> T[] arrayCopy2d(T[] original) {
    Class<?> arrayType = original.getClass().getComponentType().getComponentType();

    int[] dims = {original.length, Array.getLength(original[0])};
    T[] copy = (T[]) Array.newInstance(arrayType, dims);

    for(int i = 0; i<dims[0]; i++){
        for(int j = 0; j<dims[1], j++){
            Array.set(copy[i], j, Array.get(original[i], j));
        }
    }
    return copy; 
}
newacct
  • 119,665
  • 29
  • 163
  • 224