Type Cannot create a shared array List <FooClass>

Suppose I have a FooClass class.

public class FooClass { } 

The following line gives me the following compilation 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 thank for any help.

+4
source share
3 answers

List collection does not match 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 an array of lists, you must:

  • Create array
  • Populate this array with list instances

Example:

 List<FooClass>[] runs = new List[4]; for (int i = 0; i < runs.length; i++) { runs[i] = new ArrayList<>(); } 
+7
source

Mixing Generics and Array is not recommended. Generics does not save type information at runtime, so creating an array of generic files fails.

+2
source

The list should not be declared as an array. It should be:

 List<FooClass> runs=new ArrayList<FooClass>(4); 

or

 List<FooClass> runs=new ArrayList<FooClass>(); 

Edit : you can try List<ConfigParser> runs[] = new List[4]; . But why do you need an array of lists?

Also, as @ rai.skumar mentioned, general information is not saved at runtime due to the Erasure type.

+1
source

All Articles