-1
我试着写一个通用的SelectionSort,但是我的sort()
有错误提示,我不能调用findMinIndex(int,int)
或swap()
,虽然我可以在main()
调用。我如何修改我的代码来纠正这个问题?不能调用方法Java
public class SelectionSort<T extends Comparable<T>>{
T[] result = null;
static int size = 0;
SelectionSort(T[] a){
result = a;
size = a.length;
}
public int findMinIndex(int begin, int end){
T min = result[begin];
int index = begin;
while(begin<=end){
if(result[begin].compareTo(min)<0){
min = result[begin];
index = begin;
}
begin++;
}
return index;
}
public void swap(int i, int j){
T temp = result[i];
result[i]=result[j];
result[j]=temp;
}
private int getSize(){
return size;
}
public void sort(){
int have_sorted_till = -1;
int index = -1;
while(have_sorted_till<size){
index = result.findMinIndex(have_sorted_till+1,size-1);
result.swap(index, have_sorted_till+1);
have_sorted_till++;
}
}
public String toString(){
String s = "";
for(int i=0; i<size; i++){
s = s + result[i]+",";
}
return s;
}
public static void main(String[] args){
Double[] a = new Double[]{2.2,5.7,2.8,9.7,3.9,12.3};
SelectionSort<Double> b = new SelectionSort<Double>(a);
b.swap(0,5);
System.out.print(b.toString());
}
}
“result.findMinIndex”,它应该只是“findMinIndex”而没有“结果”。与“交换”相同。 “结果”是一个数组,而不是“SelectionSort”。 – mangoDrunk