import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
//对数组进行原地排序
int[] a = { 2, 4, 6, 8, 1, 3, 5, 7 };
sort_1(a);
print(a);
//对数组进行原地排序
int[] x = { 2, 4, 6, 8, 1, 3, 5, 7 };
int[]y = sort_2(x);
print(x);
print(y);
}
private static int[] sort_2(int[] a) {
int[]b = new int[a.length];
//复制数组a到数组b
System.arraycopy(a, 0, b, 0, a.length);
//对数组b进行排序
Arrays.sort(b);
return b;
}
private static void sort_1(int[] a) {
Arrays.sort(a);
}
private static void print(int[] b) {
for (int i : b) {
System.out.print(i+" ");
}
System.out.println();
}
}
打印出来的是:
1 2 3 4 5 6 7 8
2 4 6 8 1 3 5 7
1 2 3 4 5 6 7 8