1. 使用`Arrays.sort()`方法结合`Collections.reverseOrder()`:
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Integer[] points = {94, 53, 70, 44, 64};
Arrays.sort(points); // 对数组进行升序排序
Arrays.sort(points, Collections.reverseOrder()); // 对数组进行降序排序
System.out.println(Arrays.toString(points)); // 输出降序排列的数组
}
}
2. 使用自定义比较器实现降序排序:
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Integer[] points = {94, 53, 70, 44, 64};
Arrays.sort(points, new Comparator
() { @Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1); // 降序比较
}
});
System.out.println(Arrays.toString(points)); // 输出降序排列的数组
}
}
3. 手动实现降序排序算法,例如冒泡排序:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] points = {94, 53, 70, 44, 64};
for (int i = 0; i < points.length - 1; i++) {
for (int j = 0; j < points.length - i - 1; j++) {
if (points[j] < points[j + 1]) {
int temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(points)); // 输出降序排列的数组
}
}
以上方法都可以实现数组的降序排列。选择哪一种方法取决于你的具体需求和偏好