Java generics incompatible types (no instances of type (s) of type T)

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;

    /* 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 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());
    // todo ...
    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

+4
5

( Integer ):

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

, , ArrayList , ArrayList List, , ArrayList

:

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

: https://ideone.com/MXZxqz

+2

, inRange() List<T>. List .

:

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

:

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

:

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

lista​​p >

+2

ArrayList List

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

ArrayListis an implementation of an interface List.
Thus, all instances ArrayListare instances List, but all instances Listare optional ArrayList.

So, when you call this method:

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

you cannot assign its result ArrayListas you do it:

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

Go to the program through the interface and use Liston both sides:

List<View> inRange = Helper.inRange(...);
+2
source

Source: https://habr.com/ru/post/1677161/


All Articles