开发者

Why can i not create an arraylist of arraylist of integers by using a list of list of integer reference?

开发者 https://www.devze.com 2023-03-11 01:32 出处:网络
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 her
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>

0

精彩评论

暂无评论...
验证码 换一张
取 消