I was wondering how to initialize the generic array in the constructor.
After searching on google, I wrote the code below.
public class Test<E>{
private E[] mData;
public Test(){
mData = (E[])new Object[100];
}
public void put(E data, int index){
mData[index]=data;
}
public E get(int index){
return mData[index];
}
public static void main(String[] args){
Test<Integer> t1 = new Test<>();
t1.put(100, 0);
System.out.println(t1.get(0)); // 100
}
}
The output of the above code is expectedly 100, but if I access the generic array directly, it gives an error.
The code looks like below.
public class Test<E>{
private E[] mData;
public Test(){
mData = (E[])new Object[100];
}
public static void main(String[] args){
Test<Integer> t1 = new Test<>();
t1.mData[0] = 100;
System.out.println(t1.mData[0]);
// java.lang.ClassCastException error
}
}
The above code just gives me an ClassCastException
error.
I have no idea what's the differences between those code I have uploaded.
Any help might really be appreciated.