繁体   English   中英

Java泛型不兼容的类型(不存在类型变量T的实例)

[英]Java generics incompatible types (no instance(s) of type variable(s) T exist)

这基本上是我对Java泛型类型的第一次触摸,我无法弄清楚下面这段代码有什么问题。

我有一个带有静态函数的辅助类Helper inRange usng泛型类型,它应该从索引index处的对象周围的某个range的输入列表中返回对象列表(我还没有测试它,如果这里不是问题,它是否正常工作):

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

然后我想在课堂上使用它:

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

}
}

其中gameView.activePlayersView()的类型为ArrayList<View>

然后从我的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>

即使我将泛型类型T直接更改为View我仍然会收到此错误

最小化您的示例(使用模板化列表类型的整数):

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

即使你把模板类型放在图片之外:

ArrayList inRange = Helper.inRange(list, 0,1);

public static List inRange(List list, int index, int range) { ... }

你看到,当helper静态方法返回一个List时,你试图将它分配给一个ArrayList,这就是你的问题,因为ArrayList是List的具体实现,但是你不能将对泛型List的引用分配给具体的实现ArrayList

只需改为:

List<View> inRange = Helper.inRange(gameView.activePlayersViews(), pos, 
        playerView.range());

你很高兴: https//ideone.com/MXZxqz

ArrayListList接口的实现。
因此,所有ArrayList实例都是List实例,但所有List实例都不一定是ArrayList

所以当你调用这个方法时:

public static <T> List<T> inRange(List<T> list, int index, int range) {

你不能像以前那样将结果分配给ArrayList

ArrayList<View> inRange = Helper.inRange(...);

继续按接口编程并使用两侧的List

List<View> inRange = Helper.inRange(...);

它发生了,因为inRange()返回类型List<T> 您可以使用类型List或任何超类型将结果存储在引用中。

尝试使用此代码:

List<View> inRange = Helper.inRange(gameView.activePlayersView(), pos, 
            playerView.range());

试着做:

List<View> inRange = Helper.inRange(gameView.activePlayersView(), pos, 
        playerView.range());

并检查这个:

res.add(lista.get(i));

这个班里没有列表

您需要将ArrayList类型转换为List

ArrayList<View> inRange = Helper.inRange((List<View>) gameView.activePlayersViews(), pos, 
playerView.range());

不兼容的类型:不存在类型变量 F、T 的实例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)对getCollection()调用进行类型转换后,也不会出现上述错误 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t>

[英]incompatible types: no instance(s) of type variable(s) F,T exist so that java.util.Collection<T> conforms to java.util.Set<java.lang.Long

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM