不兼容的类型:推理变量 T 有不兼容的边界 [英] incompatible types: inference variable T has incompatible bounds

查看:36
本文介绍了不兼容的类型:推理变量 T 有不兼容的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码

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不相容的界限列表列表 = Arrays.asList(A);^等式约束:整数下限: int[] 其中 T 是类型变量:T 扩展在方法 asList(T...) 中声明的对象

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 需要可变数量的 Object.int 不是 Object,但 int[] 是,因此 Arrays.asList(A) 将创建一个List 只有一个元素.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆