This is basically my first touch with generic Java types, and I can't figure out what is wrong with the following code snippet.
I have a helper class Helperwith a static function inRangeusng generic type that should return a list of objects from the input list that are in the defined rangearound the object in the index index(I haven't tested it, this is not a problem 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;
i = (index + 1) % N;
j = 0;
while (i != index && j < range) {
res.add(list.get(i));
i = (i + 1) % N;
j++;
}
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 the 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());
return new Decision(Decision.KindOfDecision.DO_NOTHING, 0);
}
}
where gameView.activePlayersView()has type ArrayList<View>.
Then from my IDE (IntelliJ IDEA) in 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>
Even I change the generic type Tto View, I still get this error