Why first try says “Cannot create a generic array of ArrayList<String>”.

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

Answer :

If the first one is allowed to do, followig will be happend.

ArrayList<String>[] alist = new ArrayList<String>[10];
Object[] olist = alist; // This is okay because ArrayList is an Object
olist[0] = new ArrayList<Dog>();
olist[0].add(new Dog());
String s = alist[0].get(0); //Uh oh, Dog is not string

Ok versions :

ArrayList[] arryListArry = new ArrayList[4];
ArrayList<String>[] arryListArry = new ArrayLis[4];
ArrayList<ArrayList<String>> arryListArry= new ArrayList<ArrayList<String>>(4);
Advertisements