Create an Array of Arraylists


I am wanting to create an array of arraylist like below:

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

But it's not compiling. How can I do this?

This question is tagged with java arrays arraylist

~ Asked on 2011-12-19 09:09:00

The Best Answer is


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);

~ Answered on 2011-12-19 09:09:59


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];

~ Answered on 2011-12-19 09:21:22

Most Viewed Questions: