List<List<Integer>> sets = new ArrayList<ArrayList<Integer>>();
Why does the above give a compiler error and why can i 开发者_如何学JAVAnot use the generic list reference here, why do I need to make it specific arraylist reference ?
For that to compile, you would need either:
List<? extends List<Integer>> sets = new ArrayList<ArrayList<Integer>>();
or
List<List<Integer>> sets = new ArrayList<List<Integer>>();
A List<List<Integer>>
can contain any kind of List<Integer>
, such as a LinkedList<Integer>
. A List<ArrayList<Integer>>
(or ArrayList<ArrayList<Integer>>
) can only contain ArrayList<Integer>
s.
You have to do it this way:
List<? extends List<Integer>> sets = new ArrayList<ArrayList<Integer>>();
The reason is the same as why List<Integer>
is not List<Number>
精彩评论