Java – incompatible types: inference variable T has incompatible bounds

javajava-8

I have the following piece of code

public int solution(int X, int[] A) {

    List<Integer> list = Arrays.asList(A);

For some reason it's throwing the following compilation error

Solution.java:11: error: incompatible types: inference variable T has
incompatible bounds
List list = Arrays.asList(A);
^
equality constraints: Integer
lower bounds: int[] where T is a type-variable:
T extends Object declared in method asList(T…)

I assume this a Java 8 feature, but I'm not sure how to resolve the error

Best Answer

Arrays.asList is expecting a variable number of Object. int is not an Object, but int[] is, thus Arrays.asList(A) will create a List<int[]> with just one element.

You can use IntStream.of(A).boxed().collect(Collectors.toList());

Related Question