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.