Java泛型不兼容类型(不存在类型变量T的实例) [英] Java generics incompatible types (no instance(s) of type variable(s) T exist)

查看:897
本文介绍了Java泛型不兼容类型(不存在类型变量T的实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这是我第一次接触Java泛型类型,而我无法弄清楚下面的代码是怎么回事.

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.

我有一个具有静态函数inRange usng泛型类型的帮助程序类Helper,该类应返回输入列表中位于对象index周围某些range中的对象列表(我还没有还没有测试过,如果能正常工作就不是问题了.

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

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

}
}

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

然后从我的IDE(IntelliJ IDEA)调用inRange(..)的行上获得

Then from my IDE (IntelliJ IDEA) on the line calling inRange(..) I get

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,我仍然会收到此错误

Even I change generic type T directly to View I still get this error

推荐答案

最小化您的示例(使用模板化List类型的Integer):

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

您看到,当辅助静态方法返回List时,您正在尝试将其分配给ArrayList,这就是您的问题,因为ArrayList是List的具体实现,但是您不能将对通用List的引用分配给ArrayList的具体实现

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<View> inRange = Helper.inRange(gameView.activePlayersViews(), pos, 
        playerView.range());

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

这篇关于Java泛型不兼容类型(不存在类型变量T的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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