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.
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);
}
}
where gameView.activePlayersView() is of type ArrayList<View>.
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>
Even I change generic type T directly to View I still get this error