Create an Array of Arraylists

user picture user · Dec 19, 2011 · Viewed 260.5k times · Source

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?

Answer

MByD picture MByD · Dec 19, 2011

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