I’m trying to use this method but I get an error in Eclipse saying the type argument is incorrect and it tells me to change the method signature. Any reason why?
/**Creates an independent copy(clone) of the T array.
* @param array The 2D array to be cloned.
* @return An independent 'deep' structure clone of the array.
*/
public static <T> T[][] clone2DArray(T[][] array) {
int rows=array.length ;
//clone the 'shallow' structure of array
T[][] newArray = array.clone();
//clone the 'deep' structure of array
for(int row=0;row<rows;row++){
newArray[row]=array[row].clone();
}
return newArray;
}
The
copy2DArraymethod you posted appears to work as advertised. Perhaps you are calling the method incorrectly? Also make sure you are not using primitive types instead of objects in the array you are copying. In other words, useIntegerinstead ofint.Here is an example of the working method:
This code will print: