【发布时间】:2011-11-01 19:53:46
【问题描述】:
由于一般问题,我的以下代码无法编译。它来自当我尝试运行代码时。问题是我必须解决通用问题,但我无法发现是否必须更改 StAlgo 类或方法中的某些内容。
public class StAlgo{
//signature selection sort
public <T extends Comparable<T>> int selectionSort(T[] array) {
}
public static <T extends Comparable<T>> T[] getRandomPermutationOfIntegers(int size) {
T[] data = (T[])new Comparable[size];
for (Integer i = 0; i < size; i++) {
data[i] = (T)i;
}
// shuffle the array
for (int i = 0; i < size; i++) {
T temp;
int swap = i + (int) ((size - i) * Math.random());
temp = data[i];
data[i] = data[swap];
data[swap] = temp;
}
return data;
}
public <T extends Comparable<T>> void trySelectionSort(){
int N = 100, M = 100;
for(int i= 0; i < N; i++){
T[] arrayInts = (T[])new Comparable[i];
for(int j= 0; j < M; i++){
arrayInts = getRandomPermutationOfIntegers(i);
//Collections.shuffle(Arrays.asList(arrayInts));
selectionSort(arrayInts);
}
}
}
}
//Main class has the folling code:
StAlgosa s = new StAlgosa();
s.trySelectionSort();
我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Bound mismatch: The generic method trySelectionSort() of type StAlgosa is not applicable for the arguments (). The inferred type Comparable<Comparable<T>> is not a valid substitute for the bounded parameter <T extends Comparable<T>>
我该如何解决?
谢谢
【问题讨论】:
-
提供堆栈跟踪会很有用。
-
不,不是。 “未解决的编译问题”异常意味着这个类只是没有编译,但他告诉 Eclipse 无论如何都要运行该程序。行号没有意义。
标签: java