java 数组 改错问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 08:05:27
public class TestSeach {

public static void main(String[] args) {
int[] arr={3,6,9,39,0,29,90,34,57,89,98,56,66,92,21,65};

SortNumbers(arr);
}
static void SortNumbers(int[] arr){
int max,temp;
for(int i=0;i<arr.length;i++){
max=i;
for(int j=i+1;j<arr.length;j++){
if(arr[j]<arr[max]){
max=j;
}
}
if(max!=i){
temp=arr[max];
arr[max]=arr[i];
arr[i]=temp;
}
}

}

}
怎样才能让它返回数组的值?

public class TestSeach {

public static void main(String[] args) {
int[] arr = { 3, 6, 9, 39, 0, 29, 90, 34, 57, 89, 98, 56, 66, 92, 21,
65 };
int[] arr1 = new int[arr.length];
arr1 = SortNumbers(arr);
for(int i = 0;i<arr1.length;i++)
System.out.println(arr1[i]);
}

static int[] SortNumbers(int[] arr) {
int max, temp;
for (int i = 0; i < arr.length; i++) {
max = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[max]) {
max = j;
}
}
if (max != i) {
temp = arr[max];
arr[max] = arr[i];
arr[i] = temp;
}
}
return arr;
}

}
是这个吧!

使用静状数组,这样数组也不用传递了

没看出来你这个程序要实现什么。估计你自己也很迷茫
你传递的是arr这个数组的地址,所以SortNumbers执行了以后
main里的arr就变成你修改后的了

你想做冒泡吗?