Java 不兼容的类型 List of List 和 ArrayList of ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24796273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:52:36  来源:igfitidea点击:

Incompatible types List of List and ArrayList of ArrayList

javalistarraylistincomplete-type

提问by Kraken

The below line gives me error :

以下行给了我错误:

Incompatible Types.

List<List<Integer>> output = new ArrayList<ArrayList<Integer>>();

What is the reason?

是什么原因?

EDIT

编辑

I understand if I change my second ArrayList to List, it does not give me error. I want to know the reason of error though. Thanks

我明白如果我将第二个 ArrayList 更改为 List,它不会给我错误。我想知道错误的原因。谢谢

采纳答案by Dawood ibn Kareem

If you had a List<List<Integer>>then you'd be able to add a LinkedList<Integer>to it. But you can't do this for an ArrayList<ArrayList<Integer>>, so the latter can't possibly be a type of List<List<Integer>>.

如果你有一个,List<List<Integer>>那么你就可以LinkedList<Integer>向它添加一个。但是您不能为 an 执行此操作ArrayList<ArrayList<Integer>>,因此后者不可能是List<List<Integer>>.

回答by Ankit Lamba

From Generics, Inheritance, and Subtypes

泛型、继承和子类型

This is a common misunderstanding when it comes to programming with generics, but it is an important concept to learn.

enter image description here

Box<Integer>is not a subtype of Box even though Integer is a subtype of Number.

在使用泛型编程时,这是一个常见的误解,但这是一个需要学习的重要概念。

在此处输入图片说明

Box<Integer>不是 Box 的子类型,即使 Integer 是 Number 的子类型。

回答by CoderCroc

It is clearly stated in Java Doc

Java Doc 中有明确说明

In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo>is a subtype of G<Bar>. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.

一般来说,如果foo是酒吧的子类型(子类或子接口),并且G是一些通用的类型声明,它不是的情况下G<Foo>是的子类型G<Bar>。这可能是你需要学习的关于泛型的最难的事情,因为它违背了我们根深蒂固的直觉。

Same thing happens here it's Bar = List<Integer>and Foo = ArrayList<Integer>as ArrayList<ArrayList<Integer>>is not sub type of List<List<Integer>>

同样的事情发生在这里它是Bar = List<Integer>Foo = ArrayList<Integer>因为ArrayList<ArrayList<Integer>>它不是子类型List<List<Integer>>

回答by lpiepiora

The reason is that generics are not covariant.

原因是泛型不是协变的

Consider simpler case:

考虑更简单的情况:

List<Integer> integers = new ArrayList<Integer>();
List<Number> numbers = integers; // cannot do this
numbers.add(new Float(1337.44));

Now List holds a Float, which is certainly bad.

现在 List 持有一个 Float,这当然很糟糕。

Same for your case.

你的情况也一样。

List<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>();
List<List<Integer>> ll = al; // cannot do this
ll.add(new LinkedList<Integer>())

Now you have a list llwhich holds LinkedList, but the alis declared as a List of ArrayLists.

现在您有一个ll包含的列表LinkedList,但al被声明为ArrayLists的列表。

回答by spiralmoon

The correct writing should be: List<List<Integer>> ret = new ArrayList<List<Integer>>();Since in this way, you can add not only ArrayListbut also LinkedListto ret

正确的书写应该是: List<List<Integer>> ret = new ArrayList<List<Integer>>();既然这样,你不仅增加ArrayList,而且LinkedListret

回答by Pavel Shkleinik

Less text more fixes:

更少的文字更多的修复:

List<List<Integer>> lists = new ArrayList<>();

or

或者

List<List<Integer>> lists = new ArrayList<List<Integer>>();