How can I create an Array of ArrayLists? How can I create an Array of ArrayLists? arrays arrays

How can I create an Array of ArrayLists?


As per Oracle Documentation:

"You cannot create arrays of parameterized types"

Instead, you could do:

ArrayList<ArrayList<Individual>> group = new ArrayList<ArrayList<Individual>>(4);

As suggested by Tom Hawting - tackline, it is even better to do:

List<List<Individual>> group = new ArrayList<List<Individual>>(4);


As the others have mentioned it's probably better to use another List to store the ArrayList in but if you have to use an array:

ArrayList<Individual>[] group = (ArrayList<Individual>[]) new ArrayList[4];

You will need to suppress the warning but it's safe in this case.


This works:

ArrayList<String>[] group = new ArrayList[4];

Though it will produce a warning that you may want to suppress.