英文:
No suitable method found for sort
问题
我遇到了一个奇怪的Java编译器问题
以下是代码
public int[] findRightInterval(int[][] intervals) {
int n = intervals.length;
int[] intIdx = new int[n];
for(int i=0; i<n; i++) {
intIdx[i] = i;
}
Arrays.sort(intIdx, (a, b) -> (intervals[a][0] - intervals[b][0]));
...
}
完整错误信息为
第8行:错误:没有找到适合的方法来对
sort(int[],(a,b)->(in[...]][0]))
Arrays.sort(intIdx, (a, b) -> (intervals[a][0] - intervals[b][0]));
^
方法 Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) 不适用
(类型推断变量 T#1 具有不兼容的边界
相等性约束:int
下界:Object)
方法 Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) 不适用
(无法推断类型变量 T#2
(实际参数列表与形式参数列表的长度不同)) 其中 T#1、T#2 是类型变量:
T#1 扩展自Object,在方法 <T#1>sort(T#1[],Comparator<? super T#1>) 中声明
T#2 扩展自Object,在方法 <T#2>sort(T#2[],int,int,Comparator<? super T#2>) 中声明
另一方面,
如果我将 Arrays.sort(intIdx, (a, b) -> (intervals[a][0] - intervals[b][0]));
替换为 Arrays.sort(intervals, (a, b) -> (a[0] - b[0]));
就能正常工作。
想知道为什么编译器会抛出这个错误?
英文:
I run into a strange Java compiler issue
Here is the code
public int[] findRightInterval(int[][] intervals) {
int n = intervals.length;
int[] intIdx = new int[n];
for(int i=0;i<n;i++) {
intIdx[i]=i;
}
Arrays.sort(intIdx, (a, b) -> (intervals[a][0]-intervals[b][0]));
...
}
The full error is
Line 8: error: no suitable method found for
sort(int[],(a,b)->(in[...]][0]))
Arrays.sort(intIdx, (a, b) -> (intervals[a][0]-intervals[b][0]));
^
method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: int
lower bounds: Object)
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length)) where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>sort(T#1[],Comparator<? super T#1>)
T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>)
On the other hand,
if I replace Arrays.sort(intIdx, (a, b) -> (intervals[a][0]-intervals[b][0]));
with Arrays.sort(intervals, (a, b) -> (a[0]-b[0]));
it works fine.
Wonder why the compiler throw that error?
答案1
得分: 1
public static <T> void Arrays.sort(T[] a, Comparator<? super T> c)
无法使用比较器对 `int[]` 数组进行排序,只能用 `Integer[]`。因此,您的代码片段可以这样重写:
Arrays.sort(Arrays.stream(intIdx).boxed().toArray(), (a, b) -> intervals[a][0] - intervals[b][0]);
***附注:** 我建议您在示例中使用 `Stream`:*
intIdx = Arrays.stream(intIdx)
.boxed()
.sorted(Comparator.comparingInt(a -> intervals[a][0]))
.mapToInt(i -> i)
.toArray();
英文:
public static <T> void Arrays.sort(T[] a, Comparator<? super T> c)
You can't sort int[]
array with comparator. Only Integer[]
. So, your snippet could be rewritten with:
Arrays.sort(Arrays.stream(intIdx).boxed().toArray(), (a, b) -> intervals[a][0] - intervals[b][0]);
P.S. I reccoment you to use Stream
for your example:
intIdx = Arrays.stream(intIdx)
.boxed()
.sorted(Comparator.comparingInt(a -> intervals[a][0]))
.mapToInt(i -> i)
.toArray();
答案2
得分: -1
你可以导入内置的 Collections 类;
然后写入以下代码:Collections.sort(array);
英文:
you can import the built-in class Collections;
and write this:Collections.sort(array);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论