不兼容的类型:推理变量T具有不兼容的边界 [英] incompatible types: inference variable T has incompatible bounds
问题描述
我有以下代码
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:错误:不兼容的类型:推理变量T有
不兼容的边界
List list = Arrays.asList(A);
^
等式约束:整数
下限:int []其中T是一个类型变量:
T extends在方法asList(T ...)中声明的Object
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...)
我认为这是一个Java 8功能,但我不知道如何解决错误
I assume this a Java 8 feature, but I'm not sure how to resolve the error
推荐答案
Arrays.asList
期待变量数对象
。 int
不是对象
,但 int []
是,因此 Arrays.asList(A)
将只用一个元素创建一个 List< int []>
。
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.
您可以使用 IntStream.of(A).boxed()。collect(Collectors.toList());
这篇关于不兼容的类型:推理变量T具有不兼容的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!