I realized in Java, to do generic array, this is what "some" people do (because it works in some cases):
How to create a generic array?
Those answers (or other online answers) generally say there are two ways of doing it, one is do type conversion for Object[], the other is to use Arrsy.newInstance(), a reflection, But reflection requires import Java.reflect.. etc. which i probably can not do now (for restriction reasons).
MyType[] newArray = (MyType[]) new Object[size];
But, I got error at runtime:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LMyClass$MyType;
As you have seen, MyType is a private class defined in MyClass. Also, MyClass is defined as :
class MyClass<K,V> {
private class MyType {
public K data;
public V data;
}
pass;
}
I guess the casting failed because of: 1, nested class definition 2, generic type in the nested definition?
Could anyone shine light on this issue? thx