3

我想在 main 方法中调用代码,但它似乎并没有真正起作用。

public class RecursiveSelectionSort {

public static void sort(double [] list){


    sort(list, 0, list.length -1);
}
public static void sort(double [] list, int low, int high){
    if (low < high){
        int indexOfMin = low;
        double min = list[low];
        for(int i = low + 1; i <= high; i++){
            if(list[i]< min){
                min = list[i];
                indexOfMin = i;
            }
        }
        list[indexOfMin] = list[low];
        list[low] = min;

        sort(list, low + 1, high);
    }
}
public static void main(String [] args){
    double [] list = {3.0,4.0,1.0};//I tried but it doesn't work
    System.out.print(sort(list));//it doesn't go to sort(list);
}
}

我想知道我是否可以调用它。这看起来很容易,但确实令人困惑。

4

5 回答 5

5

该方法sort返回void,但您试图将其结果传递给print. 试试这个:

double [] list = {3.0,4.0,1.0};
sort(list);
System.out.print(Arrays.toString(list));

请注意,我还使用了实用程序方法Arrays.toString,这是必要的,因为数组本身不会覆盖toString以显示其内容。

于 2012-12-02T15:32:01.737 回答
1

sort是一种void方法,你不能打印它的结果。先尝试sorting,然后单独打印列表。

于 2012-12-02T15:32:11.293 回答
0

println函数不支持void参数。IT 期望一些非 void对象作为输入。但是你的排序函数什么也不返回。这就是它不打印的原因。

于 2012-12-02T15:30:59.347 回答
0
public class Mainn {

    public static void sort(double [] list){
            //change 1
        System.out.println("Inside sort");
        sort(list, 0, list.length -1);
    }
    public static void sort(double [] list, int low, int high){
        if (low < high){
            int indexOfMin = low;
            double min = list[low];
            for(int i = low + 1; i <= high; i++){
                if(list[i]< min){
                    min = list[i];
                    indexOfMin = i;
                }
            }
            list[indexOfMin] = list[low];
            list[low] = min;

            sort(list, low + 1, high);
        }
    }
    public static void main(String [] args){
        double [] list = {3.0,4.0,1.0};//I tried but it doesn't work

        //change 2
        sort(list);
        //return type of sort was void

        //print sorted list here
        System.out.println(Arrays.toString(list));
    }
}
于 2012-12-02T15:34:12.683 回答
0

试试这个

public class RecursiveSelectionSort {

    public static double[] sort(double [] list){
    return sort(list, 0, list.length -1);
    }
    public static double[] sort(double [] list, int low, int high){
        if (low < high){
            int indexOfMin = low;
            double min = list[low];
            for(int i = low + 1; i <= high; i++){
                if(list[i]< min){
                    min = list[i];
                    indexOfMin = i;
                }
            }
            list[indexOfMin] = list[low];
            list[low] = min;
            return sort(list, low + 1, high);
        }
            else return list;
    }
    public static void main(String [] args){
        double [] list = {3.0,4.0,1.0};
        list = sort(list);
        for (int i = 0; i < list.length; i++)
            System.out.println(list[i]);
    }
}
于 2012-12-02T15:46:14.640 回答