I am looking to reinvent the wheel a little and create my own generic array-backed list class in Java similar to ArrayList. Yes I know this is silly, but it is an academic pursuit. The problem is that you cannot instantiate an array of a generic type
public class MySuperCoolList<E> {
E[] array;
public MySuperCoolList<E> () {
array = new E[10]; // ERROR: cannot do this!
}
}
Surely there must be a solution to this problem because Java's ArrayList is doing the same thing. The question is, how? How can I instantiate an array of a generic type E
? And how is it done in ArrayList (if anyone knows)?