EDIT: This is NOT a duplicate of the above question. The original question concerns a list whose type is unknown at compile time. This question concerns an array like construct of a generic list.
For example the above solution of final E[] a = (E[]) Array.newInstance(c, s);
won't work because I can not obtain List<MyClass>.class
And also you can't cast from Object[]
to List<MyClass>[]
What is the idiomatic way to deal with an array like construct of generic type? For example, I want an array of List<MyClass>
. ArrayList<List<MyClass>> myClassArray
won't work directly because I need to be able to do things like myClassArray.set(5, myClassObj)
even when the myClassArray.size()
is 0. Initialize it with
for(int i = 0; i < mySize; i ++){
myClassArray.add(null)
}
works but it's ugly.
List<MyClass> myClassArray[]
won't work either because you can't create a generic array. So what is the most idiomatic way to achieve my goal? The size of array won't change once created.