I need to make a Deep Copy of a 2D array. I know how to do this
int[][] a = new int[original.length][];
for(int i = 0; i < original.length; i++) {
a[i] = Arrays.copyOf(original[i], original[i].length);
}
However, I'm now in a position where I don't know the Object type of the Array I'm copying. I have a method like so
public Object[] copy(Object[] original) {}
Which should return a deep copy of original IF it is an array of arrays. If the method is given a int[][], I can't cast that to an Object for the array (since it's a primitive type).
Currently, I can check to see if 'original' is a 2D array like so
if(original==null||original.length==0||!original.getClass().getName().startsWith("[")) {
return null;
}
But now I'm unsure on how to do a Deep copy of an array of an unknown type. I assume I'll be needing to use Java Reflection, although I'm not 100% sure on how I would go about this.
Aucun commentaire:
Enregistrer un commentaire