java List<Integer> 不能转换为 ArrayList<Integer>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31754638/
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-11-02 19:04:41  来源:igfitidea点击:

List<Integer> cannot be converted to ArrayList<Integer>

javalisttypeconverter

提问by beepretty

At the beginning of my code, there is:

在我的代码的开头,有:

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

And then, (here is to reverse a sub-list):

然后,(这里是反转一个子列表):

List<Integer> sub = new ArrayList();
re = reverse(result.get(j)); // error occurs here

There is this method:

有这个方法:

public ArrayList<Integer> reverse(ArrayList<Integer> list) {
    List<Integer> newList = new ArrayList<>();

    for(int i=list.size()-1; i>=0; i--) {
        newList.add(list.get(i));}
        return newList;
    }
}

The error message is:

错误信息是:

List cannot be converted to ArrayList

列表无法转换为 ArrayList

Why?

为什么?

回答by Captain Man

Think of it like this, you have a Fruitcalled re(I use this name because it's the name of the variable you are using).

可以这样想,您有一个Fruit调用re(我使用这个名称是因为它是您正在使用的变量的名称)。

Fruit re;

You have a method reversewhose input type is Apple.

您有一个reverse输入类型为的方法Apple

public Apple reverse(Apple a) {
    // ...
}

We have a variable rethat we declared as Fruitwhich means we're saying it's always going to be some kind of Fruit, perhaps Apple, but maybe Orange-- or even Banana.

我们有一个re我们声明为的变量,Fruit这意味着我们说它总是会是某种Fruit,也许是Apple,但也许Orange- 甚至是Banana

When you try to give the Fruitto the method taking Applethe compiler stops you, because it can't be certain that it's 100% an Apple. For example...

当您尝试给予Fruit服用方法Apple编译器停止你,因为它不能确定它是100%的Apple。例如...

Fruit re = new Orange();
reverse(re);

Yikes! We are putting a square peg into a round hole so to speak. reversetakes Apple, not Orange. Bad things could happen!

哎呀!可以这么说,我们正在将一个方钉插入一个圆孔中。reverse需要Apple,不是Orange。可能会发生不好的事情!

Side note:Why is it okay then to assign Appleto something declared as Fruitthen? (reversereturns an Apple, Fruit f = reverse(re);is legal.) Because an Appleisa Fruit. If it were declared as the more specific Appleand the return type were the more general Fruit, thenthere would be an issue here. (If reversereturned Fruit, Apple a = reverse(re);would be illegal.)

旁注:为什么可以分配Apple给那样声明的东西Fruit?(reverse返回 an Apple,Fruit f = reverse(re);是合法的。)因为 anApplea Fruit。如果它被声明为更具体的Apple并且返回类型更通用Fruit那么这里就会出现问题。(如果reverse返回FruitApple a = reverse(re);将是非法的。)

If you didn't follow the metaphor,replace Fruitwith Listand Applewith ArrayListand read the above again. Listis Fruit, a general way to describe an abstract idea. ArrayListis Apple, a specific implementation of the abstract idea. (LinkedListcould be Orangetoo.)

如果你没有按照比喻,替换FruitList,并AppleArrayList以上再次读取。ListFruit,描述抽象思想的一般方式。ArrayListApple,抽象思想的具体实现。(LinkedList也可能是Orange。)

In general you want to declare things as the most general thing you can to get the functionality you need. Making the below change should fix your problem.

通常,您希望将事物声明为获得所需功能的最一般事物。进行以下更改应该可以解决您的问题。

public List<Integer> reverse(List<Integer> list) {

We are taking some kind of Listof Integers and returning some kind of Listof Integers.

我们正在采取某种ListIntegerS和返回某种ListInteger秒。

回答by Reut Sharabani

You're using a specific implementation of the Listinterface as a return type for the reversefunction (namely ArrayList). This forces what you return to be an ArrayList, but you're defining it as List.

您正在使用List接口的特定实现作为reverse函数的返回类型(即ArrayList)。这会强制您返回的内容为ArrayList,但您将其定义为List

To understand, try and change the construction part to use a LinkedList, which also implements List:

要理解,请尝试将构造部分更改为使用 a LinkedList,它还实现了List

List<Integer> newList = new LinkedList<Integer>();

This construction is valid, but now we try and return a LinkedListfrom a function that returns an ArrayList. This makes no sense since they aren't related (But notice this: they both implement List).

这种构造是有效的,但现在我们尝试LinkedList从返回 a的函数中返回 a ArrayList。这是没有意义的,因为它们不相关(但请注意:它们都实现了List)。

So now you have two options:

所以现在你有两个选择:

Make the implementation more specific by forcing use of ArrayLists:

通过强制使用ArrayLists使实现更加具体:

ArrayList<Integer> newList = new ArrayList<>();

Or better, make the implementation more general by changing the function's return type (any Listcan be plugged in):

或者更好的是,通过更改函数的返回类型(List可以插入任何类型)使实现更通用:

public List<Integer> reverse(List<Integer> list){

回答by StansTec

It's because your result varible has a type of List, but your method want an ArrayList Try it with:

这是因为你的结果变量有一个 List 类型,但你的方法想要一个 ArrayList 试试看:

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

orchange your function in:

更改您的功能:

public List<Integer> reverse(List<Integer> list){...