这基本上是我第一次接触 Java 泛型类型,我无法弄清楚下面的代码有什么问题。
我有一个助手类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;
}
}
然后我想在课堂上使用它:
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()
属于类型ArrayList<View>
.
然后从我的IDE(IntelliJ IDEA)上调用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
我仍然收到此错误