Java 泛型不兼容类型(不存在类型变量 T 的实例)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43976960/
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
Java generics incompatible types (no instance(s) of type variable(s) T exist)
提问by micsza
That's basically my first touch with Java generic types and I can't figure out what is wrong with the following piece of code.
这基本上是我第一次接触 Java 泛型类型,我无法弄清楚以下代码有什么问题。
I have a helper class Helper
with a static function inRange
usng generic type that should return the list of objects from an input list that are in certain range
around object at index index
(I haven't tested it yet, it's not an issue here if it works correctly or not):
我有一个Helper
带有静态函数的辅助类,inRange
它应该返回一个输入列表中的对象列表,这些range
对象在索引处的某个对象周围index
(我还没有测试过,如果它正常工作或不是):
public class Helper {
public static <T> List<T> inRange(List<T> list, int index, int range) {
List<T> res = new ArrayList<T>();
int N = list.size();
assert(index < N);
if (N == 0)
return res;
int i, j;
/* right range */
i = (index + 1) % N;
j = 0;
while (i != index && j < range) {
res.add(list.get(i));
i = (i + 1) % N;
j++;
}
/* left range */
i = (N + index - 1) % N;
j = 0;
while (i != index && j < range && !res.contains(list.get(i))) {
res.add(lista.get(i));
i = (N + i - 1) % N;
j++;
}
return res;
}
}
Then I want to use it in a class:
然后我想在课堂上使用它:
import java.util.ArrayList;
public class StrategyA extends StrategyB {
public Decision makeDecision(GameView gameView, Action action, View playerView) {
int pos = gameView.activePlayersViews().indexOf(playerView);
assert(pos != -1);
ArrayList<View> inRange = Helper.inRange(gameView.activePlayersViews(), pos,
playerView.range());
// todo ...
return new Decision(Decision.KindOfDecision.DO_NOTHING, 0);
}
}
where gameView.activePlayersView()
is of type ArrayList<View>
.
哪里gameView.activePlayersView()
是类型ArrayList<View>
。
Then from my IDE (IntelliJ IDEA) on the line calling inRange(..)
I get
然后从我的 IDE (IntelliJ IDEA) 上调用inRange(..)
我得到
Error:(8, 56) java: incompatible types: no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to java.util.ArrayList<View>
Error:(8, 56) java: incompatible types: no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to java.util.ArrayList<View>
Even I change generic type T
directly to View
I still get this error
即使我T
直接将泛型类型更改为View
我仍然收到此错误
采纳答案by guido
Minimize your example like (using Integer of the templated List type):
最小化您的示例(使用模板化列表类型的整数):
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
List<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> inRange = Helper.inRange(list, 0,1);
}
}
class Helper {
public static <T> List<T> inRange(List<T> list, int index, int range) {
List<T> res = new ArrayList<T>();
return res;
}
}
Then even if you put template types out of the picture:
那么即使您将模板类型排除在外:
ArrayList inRange = Helper.inRange(list, 0,1);
public static List inRange(List list, int index, int range) { ... }
you see that while the helper static method returns a List, you are trying to assign it to an ArrayList, and that's your problem, as ArrayList is a concrete implementation of List, but you cannot assign a reference to a generic List to a concrete implementation of ArrayList
您会看到,虽然辅助静态方法返回一个 List,但您正试图将其分配给 ArrayList,这就是您的问题,因为 ArrayList 是 List 的具体实现,但您不能将泛型 List 的引用分配给具体实现ArrayList
Just change to:
只需更改为:
List<View> inRange = Helper.inRange(gameView.activePlayersViews(), pos,
playerView.range());
and you are good to go: https://ideone.com/MXZxqz
你很高兴去:https: //ideone.com/MXZxqz
回答by Vladimir Parfenov
It happens, because inRange()
returns type List<T>
. You can store result in reference with type List
or any supertype.
它发生了,因为inRange()
返回 type List<T>
。您可以将结果存储在类型List
或任何超类型的引用中。
Try to use this code:
尝试使用此代码:
List<View> inRange = Helper.inRange(gameView.activePlayersView(), pos,
playerView.range());
回答by Razikus
Try to do:
试着做:
List<View> inRange = Helper.inRange(gameView.activePlayersView(), pos,
playerView.range());
And check this:
并检查这个:
res.add(lista.get(i));
there is no lista in this class
这个班级没有lista
回答by SalaryNotFound
You need to cast the type ArrayList
to be List
您需要将类型ArrayList
转换为List
ArrayList<View> inRange = Helper.inRange((List<View>) gameView.activePlayersViews(), pos,
playerView.range());
回答by davidxxx
ArrayList
is an implementation of List
interface.
So all ArrayList
instances are List
instances but all List
instances are not necessarily ArrayList
.
ArrayList
是List
接口的实现。
因此,所有ArrayList
实例都是List
实例,但List
并非所有实例都必须是ArrayList
。
So when you call this method :
所以当你调用这个方法时:
public static <T> List<T> inRange(List<T> list, int index, int range) {
you cannot assign its result to an ArrayList
as you are doing :
您不能将其结果分配给ArrayList
您正在做的事情:
ArrayList<View> inRange = Helper.inRange(...);
Go on to program by interface and use List
in both sides :
继续按接口编程,List
两边使用:
List<View> inRange = Helper.inRange(...);