Arrsys.sort()是从小到大排序,要想降序排列需要用到Comparator。
注意如果是int arr[]会发生报错,必须使用Integer arr[]
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Integer arr[] = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int a = in.nextInt();
//升序
if (a == 0) {
Arrays.sort(arr);
}
//降序
if (a == 1) {
Arrays.sort(arr, new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
}
for (int i : arr) {
System.out.print(i + " ");
}
}
}