[英]Java generics: Wildcard capture compilation error
I get following compilation errors: 我收到以下编译错误:
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<capture#1-of ? extends T>) Main.java /Sorting/src/com/kash/test line 11
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<capture#2-of ? super T>) Main.java /Sorting/src/com/kash/test line 15
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<T>) Main.java /Sorting/src/com/kash/test line 19
when I compile (with JDK 1.7.0 in Eclipse Juno) this code: 当我编译(使用Eclipse Juno中的JDK 1.7.0)时,此代码:
package com.kash.test;
import java.util.Collections;
import java.util.Comparator;
// 3 attempts to write a wrapper over: Collections' method
// public static <T> void sort(List<T> list, Comparator<? super T> c)
public class Main {
public static <T> void sort1(T[] array, Comparator<? extends T> c) {
Collections.sort(array, c); // line 11
}
public static <T> void sort2(T[] array, Comparator<? super T> c) {
Collections.sort(array, c); // line 15
}
public static <T> void sort3(T[] array, Comparator<T> c) {
Collections.sort(array, c); // line 19
}
public static void main(String[] args) {
}
}
I have read following but did not really understand much. 我已阅读以下内容,但并不太了解。 Any help?
有什么帮助吗?
-- edit -- -编辑-
For the picky guys who would want to know why am I doing such stupid things. 对于那些想知道我为什么要做这些愚蠢的事情的挑剔的家伙。 If you don't care why am I doing this, don't bother the rest.
如果您不在乎我为什么要这样做,请不要打扰其余的人。
Why? 为什么?
I'm writing several implementations (eg Quick, Insertion, Heap, Mixed, Intro, Bubble, ...) of following interface: 我正在编写以下接口的几种实现(例如,快速,插入,堆,混合,简介,气泡等):
package com.kash.src;
import java.util.Comparator;
import java.util.List;
/**
* Interface that provides all sorting functions for all possible representations of a list of Objects (non-primitive
* types). <br>
* - {@link List}<{@link Comparable}><br>
* - Array of < ? extends {@link Comparable}><br>
* - {@link List}< ? > with external {@link Comparator}<br>
* - Array of < ? > with external {@link Comparator}<br>
*
* @author Kashyap Bhatt
*
*/
public interface Sorter {
<T extends Comparable<T>> void sort(List<T> list);
<T extends Comparable<T>> void sort(T[] array);
<T extends Object> void sort(List<T> list, Comparator<? super T> c);
<T extends Object> void sort(T[] array, Comparator<? super T> c);
}
So that I can test all my sort implementations and test them. 这样我就可以测试所有排序实现并对其进行测试。 I wanna compare the results with Java's sort implementations, so I'm also writing an implementation of this interface which internally just calls Java's sort methods.
我想将结果与Java的sort实现进行比较,因此我还要编写此接口的实现,该接口在内部仅调用Java的sort方法。 That's where I faced the problem.
那就是我面临的问题。
Collections#sort expects a List
while you are passing an array. 在传递数组时, Collections#sort需要一个
List
。
Do: 做:
public static <T> void sort1(T[] array, Comparator<? extends T> c) {
Collections.sort(Arrays.asList(array), c); // line 11
}
Collections.sort()
不会对数组进行排序,而是对List
排序。
The method
sort(List<T>, Comparator<? super T>)
in the type Collections is not applicable for the arguments(T[], Comparator<capture#1-of ? extends T>)
Main.java /Sorting/src/com/kash/test line 11Collection。类型中的方法
sort(List<T>, Comparator<? super T>)
不适用于以下参数(T[], Comparator<capture#1-of ? extends T>)
Main.java / Sorting / src / com / kash /测试行11
You are passing T[]
where List<T>
is expected and Comparator<? extends T>
您正在传递
T[]
,其中期望List<T>
和Comparator<? extends T>
Comparator<? extends T>
where Comparator<? super T>
Comparator<? extends T>
Comparator<? super T>
Comparator<? super T>
is expected. Comparator<? super T>
是预期的。
Why do you think it should not give error? 您为什么认为它不应该出错?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.