地址:输入整型数组和排序标识,对其元素按照升序或降序进行排序_牛客题霸_牛客网
示例代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
Collections.sort(list);
if (sc.nextInt() == 1) {
Collections.reverse(list);
}
for (Integer integer : list) {
System.out.print(integer + " ");
}
}
1. Collections.sort(list) 方法,将list排序;
2. Collections.reverse(list) 将list中值的顺序反转。