Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incompatible types: inference variable T has incompatible bounds [duplicate]

Tags:

java

java-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

like image 437
PDStat Avatar asked Dec 17 '14 09:12

PDStat


1 Answers

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());

like image 187
tobias_k Avatar answered Sep 18 '22 16:09

tobias_k