List< Integer>无法转换为ArrayList< Integer> [英] List<Integer> cannot be converted to 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
为什么?
推荐答案
像这样,您有一个名为re
的Fruit
(我使用此名称,因为它是您正在使用的变量的名称).
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
返回Apple
,Fruit 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 asFruit
then? (reverse
returns anApple
,Fruit f = reverse(re);
is legal.) Because anApple
is aFruit
. If it were declared as the more specificApple
and the return type were the more generalFruit
, then there would be an issue here. (Ifreverse
returnedFruit
,Apple a = reverse(re);
would be illegal.)
如果您没有按照隐喻进行操作,请 将Fruit
替换为List
,将Apple
替换为ArrayList
,然后再次阅读以上内容. List
是Fruit
,是描述抽象概念的一般方法. ArrayList
是Apple
,是抽象概念的特定实现. (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) {
我们正在使用某种List
的Integer
,并返回某种List
的Integer
s.
We are taking some kind of List
of Integer
s and returning some kind of List
of Integer
s.
这篇关于List< Integer>无法转换为ArrayList< Integer>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!