gpt4 book ai didi

java 调用 main 方法数组参数(简单但令人困惑)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:50:38 26 4
gpt4 key购买 nike

本想调用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);
}
}

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

最佳答案

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

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

注意我还使用了实用方法Arrays.toString ,这是必需的,因为数组本身不会覆盖 toString 来显示它们的内容。

关于java 调用 main 方法数组参数(简单但令人困惑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13670688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com