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 ?
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>>();
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.
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