题解 | #输入整型数组,对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
使用了List
import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<Integer> val = new ArrayList<>(); while (n-- > 0) { val.add(in.nextInt()); } val.sort(in.nextInt() == 0 ? Comparator.naturalOrder() : Comparator.reverseOrder()); System.out.println(val.stream().map(String::valueOf).collect(Collectors.joining(" "))); } }