4 回答

TA貢獻1921條經驗 獲得超9個贊
您的行Arrays.sort(i,Collections.reverseOrder());不會編譯,因為數組不是集合。使用 aList而不是數組并像這樣使用它:
public static void main(String[] argu) {
List<Integer> i = new ArrayList<>();
Scanner sc = new Scanner(System.in);
for (int j = 0; j <= 9; j++) {
i.add(Integer.valueOf(sc.nextLine()));
}
System.out.println("Sorted:");
Collections.sort(i);
i.forEach(System.out::println);
System.out.println("\nReversed:");
Collections.sort(i, Collections.reverseOrder());
i.forEach(System.out::println);
}

TA貢獻1891條經驗 獲得超3個贊
Arrays.sort(i, Collections.reverseOrder())
不適用于原語。如果您需要使用上述方法進行排序,請嘗試將值讀取為Integer
not int
。
如果您需要使用原語,請使用一個簡單的比較器并將其傳遞給Arrays.sort()
或使用如下所示的內容:Collections.sort(i, (int a, int b) -> return (b-a));

TA貢獻1877條經驗 獲得超1個贊
或者使用流:
i = Arrays.stream(i).boxed() .sorted(Comparator.reverseOrder()) .mapToInt(Integer::intValue) .toArray()
添加回答
舉報