In Java, why doesn’t the following line of code work?
List<List<String>> myList = new ArrayList<ArrayList<String>>();
It works if I change it to
List<ArrayList<String>> myList = new ArrayList<ArrayList<String>>();
At first, I thought maybe you can’t have lists of an interface, but I can create a List<Runnable> just fine.
Ideas?
Generic types are more pedantic.
ListmeansListor any sub-type, but<List>means onlyList. If you want a sub-type you need to have<? extends List>I suspect you can use
The reason you can’t do this is that you can be using a reference to a reference and with an extra level of indirection you have to be careful.
With generics you can have two level of indirection which can give you problems so they are more pedantic to avoid these issues.
prints