问题
The below code :
List<List<String>> lss = new ArrayList<ArrayList<String>>();
causes this compile time error :
Type mismatch: cannot convert from ArrayList<ArrayList<String>> to List<List<String>>
to fix I change the code to :
List<ArrayList<String>> lss = new ArrayList<ArrayList<String>>();
Why is this error being thrown ? Is this because the generic type List in List<List<String>>
is instantiated and since List is an interface this is not possible ?
回答1:
The problem is that type specifiers in generics do not (unless you tell it to) allow subclasses. You have to match exactly.
Try either:
List<List<String>> lss = new ArrayList<List<String>>();
or:
List<? extends List<String>> lss = new ArrayList<ArrayList<String>>();
回答2:
From http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
Note: Given two concrete types A and B (for example, Number and Integer),
MyClass<A>
has no relationship toMyClass<B>
, regardless of whether or not A and B are related. The common parent ofMyClass<A>
andMyClass<B>
is Object.

回答3:
Same reason
List<List> myList = new ArrayList<ArrayList>();
wont work.
You can see this question for more details.
来源:https://stackoverflow.com/questions/20702635/why-does-this-not-compile-listliststring-lss-new-arraylistarrayliststr