I have always had this one issue with arrays of ArrayLists. Maybe you can help.
//declare in class
private ArrayList<Integer>[] x;
//in constructor
x=new ArrayList[n];
This generates a warning about unchecked conversion.
But
x=new ArrayList<Integer>[n];
is a compiler error.
Any idea?
Thanks!
I think you cannot make array of generic arraylist because no generic information will be available at runtime.Instead you can do like this:
List<Integer>[] arr=new ArrayList[30];
arr[0]=new ArrayList<Integer>();//create new arraylist for every index.
You can't make a array of generics lists. Fortunately, there are workarounds. And even more fortunately, there is a nice site about Generics with more information than you'd ever want to know. The link goes straight to the Arrays in Java Generics part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With