List< Integer>无法转换为ArrayList< Integer> [英] List<Integer> cannot be converted to ArrayList<Integer>

查看:514
本文介绍了List< Integer>无法转换为ArrayList< Integer>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码开头,有:

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

有这种方法:

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;
    }
}

错误消息是:

列表无法转换为ArrayList

List cannot be converted to ArrayList

为什么?

推荐答案

像这样,您有一个名为reFruit(我使用此名称,因为它是您正在使用的变量的名称).

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

Fruit re;

您有一个方法reverse,其输入类型为Apple.

You have a method reverse whose input type is Apple.

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

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

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

当您尝试将Fruit赋予采用Apple的方法时,编译器会阻止您,因为无法确定它是Apple的100%.例如...

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

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

赞!可以这么说,我们正在将一个方形的钉子插入一个圆孔中. reverse需要Apple,而不是Orange.可能发生坏事!

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

旁注:那么为什么可以将Apple分配给声明为Fruit的对象呢? (reverse返回AppleFruit f = reverse(re);是合法的.)因为Apple Fruit.如果将其声明为更具体的Apple,并且将返回类型指定为更通用的Fruit,则 then 会出现问题. (如果reverse返回Fruit,则Apple a = reverse(re);将是非法的.)

Side note: Why is it okay then to assign Apple to something declared as Fruit then? (reverse returns an Apple, Fruit f = reverse(re); is legal.) Because an Apple is a Fruit. If it were declared as the more specific Apple and the return type were the more general Fruit, then there would be an issue here. (If reverse returned Fruit, Apple a = reverse(re); would be illegal.)

如果您没有按照隐喻进行操作,请 Fruit替换为List,将Apple替换为ArrayList,然后再次阅读以上内容. ListFruit,是描述抽象概念的一般方法. ArrayListApple,是抽象概念的特定实现. (LinkedList也可能是Orange.)

If you didn't follow the metaphor, replace Fruit with List and Apple with ArrayList and read the above again. List is Fruit, a general way to describe an abstract idea. ArrayList is Apple, a specific implementation of the abstract idea. (LinkedList could be Orange too.)

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

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) {

我们正在使用某种ListInteger,并返回某种ListInteger s.

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

这篇关于List&lt; Integer&gt;无法转换为ArrayList&lt; Integer&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆