Suppose I have class FooClass.
public class FooClass {
}
The following line gives me the following compile error:
// Note I want to create an array of length 4 of Lists of FooClass
List<FooClass> runs[]=new List<FooClass>[4];
Cannot create a generic array of List<FooClass> ...
Would appreciate any help.
List collection is not the same as array:
// if you want create a List of FooClass (you can use any List implementation)
List<FooClass> runs = new ArrayList<FooClass>();
// if you want create array of FooClass
FooClass[] runs = new FooClass[4];
UPD:
If you want to create array of lists, you should:
Example:
List<FooClass>[] runs = new List[4];
for (int i = 0; i < runs.length; i++) {
runs[i] = new ArrayList<>();
}
It's not good idea to mix Generics and Array. Generics doesn't retain type information at run time so creating an array of generics fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With