Trying to create a generic array
I am getting an "incompatible types" compile time error, even though
to me (and apparently to the compiler too) these are just three forms
to refer to the same class:
1) ai.class;
2) Class.forName("ai");
3) ((Class.forName("ai")).newInstance()).getClass();
Here is the actual code example:
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
import java.util.*;
import java.lang.reflect.*;
class ai{
String a;
int i;
}
class gnrx10{
gnrx10(){}
public <T> T[] createTypeAr(Class<T> Tp, int iSz){
T[] TpAr = (T[])Array.newInstance(Tp, iSz);
return(TpAr);
}
}
public class gnrx10Test{
public static void main(String args[]){
gnrx10 g = new gnrx10();
// __ this works fine!
String[] aSAr = g.createTypeAr(String.class, 4);
System.out.println("// __ aSAr.length: |" + aSAr.length + "|");
ai[] aiAr = g.createTypeAr(ai.class, 16);
System.out.println("// __ aiAr.length: |" + aiAr.length + "|");
// __
try{
System.out.println(ai.class);
System.out.println(Class.forName("ai"));
System.out.println(((Class.forName("ai")).newInstance()).getClass());
// __ this doesn't!
/*
Class K = ai.class;
System.out.println(K);
aiAr = g.createTypeAr(K, 8);
System.out.println("// __ aiAr.length: |" + aiAr.length + "|");
gnrx10Test.java:47: incompatible types
found : java.lang.Object[]
required: ai[]
aiAr = g.createTypeAr(K, 8);
^
*/
}catch(ClassNotFoundException KNFX){ KNFX.printStackTrace(); }
catch(InstantiationException InstX){ InstX.printStackTrace(); }
catch(IllegalAccessException IlgAxX){ IlgAxX.printStackTrace(); }
}
}