public static void main(String[] args) {
// 定义含有5个元素的数组
double[] scores = new double[] { 78, 45, 85, 97, 87 };
System.out.println("排序前数组内容如下:");
// 对scores数组进行循环遍历
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
System.out.println("\n排序后的数组内容如下:");
// 对数组进行排序
Arrays.sort(scores);
// 遍历排序后的数组
for (int j = 0; j < scores.length; j++) {
System.out.print(scores[j] + "\t");
}
}
如上述代码所示,要对一个数组进行升序排列,只需要调用 Arrays.sort() 方法即可。运行后的输出结果如下所示。
排序前数组内容如下: 78.0 45.0 85.0 97.0 87.0 排序后的数组内容如下: 45.0 78.0 85.0 87.0 97.0
public static void main(String[] args) {
Integer[] a = { 9, 8, 7, 2, 3, 4, 1, 0, 6, 5 }; // 数组类型为Integer
Arrays.sort(a, Collections.reverseOrder());
for (int arr : a) {
System.out.print(arr + " ");
}
}
输出结果如下:
9 8 7 6 5 4 3 2 1 0
2)实现 Comparator 接口的复写 compare() 方法,代码如下:
public class Test {
public static void main(String[] args) {
/*
* 注意,要想改变默认的排列顺序,不能使用基本类型(int,double,char)而要使用它们对应的类
*/
Integer[] a = { 9, 8, 7, 2, 3, 4, 1, 0, 6, 5 };
// 定义一个自定义类MyComparator的对象
Comparator cmp = new MyComparator();
Arrays.sort(a, cmp);
for (int arr : a) {
System.out.print(arr + " ");
}
}
}
// 实现Comparator接口
class MyComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
/*
* 如果o1小于o2,我们就返回正值,如果o1大于o2我们就返回负值, 这样颠倒一下,就可以实现降序排序了,反之即可自定义升序排序了
*/
return o2 - o1;
}
}
输出结果如下所示。
9 8 7 6 5 4 3 2 1 0
注意:使用以上两种方法时,数组必须是包装类型,否则会编译不通过。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有