I need to convert primitive array (ex. int[].class) to its wrapper array (ex. Integer[].class). java.util.Arrays.copyOfRange() (in Java 1.6) produces a classCastException.
So, I had to develop my own version of java.util.Arrays.copyOfRange() as follows:
private static Object CopyOfRange (
Object Source[],
Class ArrayCompType )
{
Object Obj;
Obj = java.lang.reflect.Array.newInstance ( ArrayCompType,Source.length );
for ( int i=0; i < Source.length; i++ )
if ( Source[i] != null )
try {
java.lang.reflect.Array.set ( Obj,i,Source[i] );
}
catch ( Exception e )
{
return null;
}
return Obj;
}
This method converts any array to the new type except when you call it with:
int i[]={1,2,3};
Object[] o;
o=(Object[])CopyOfRange(i,Object.class);
this does not even compile because i is a primitive array and cannot be casted to Object array.
I thought of using Class.cast(), but couldn't get the syntax right
Thank you