Incompatible types List of List and ArrayList of ArrayList
下面的一行给出了错误:
1 2 3 |
原因是什么?
编辑
我知道如果我把第二个数组列表改成列表,它不会给我带来错误。不过,我想知道出错的原因。谢谢
来自泛型、继承和子类型
This is a common misunderstanding when it comes to programming with
generics, but it is an important concept to learn.
Box is not a subtype of Box even though Integer is a subtype of Number.
如果你有一个>
>
正确的文字应该是:> ret = new ArrayList
>();
原因是泛型不是协变的。
考虑更简单的情况:
1 2 3 | List<Integer> integers = new ArrayList<Integer>(); List<Number> numbers = integers; // cannot do this numbers.add(new Float(1337.44)); |
现在列表中有一个浮动,这肯定是不好的。
你的情况也一样。
1 2 3 | List<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>(); List<List<Integer>> ll = al; // cannot do this ll.add(new LinkedList<Integer>()) |
现在您有一个包含
它在Java文档中有明确的表述。
In general, if Foo is a subtype (subclass or subinterface) of Bar, and
G is some generic type declaration, it is not the case thatG is
a subtype ofG . This is probably the hardest thing you need to
learn about generics, because it goes against our deeply held
intuitions.
同样的事情也发生在这里,它是>
更少的文本更多的修复:
1 | List<List<Integer>> lists = new ArrayList<>(); |
或
1 | List<List<Integer>> lists = new ArrayList<List<Integer>>(); |