方法一:反向遍历
import java.util.Arrays;public class ArraysDemo { public static void main(String[] args) { //定义int数组并赋值 int[] arr = {12,15,32,16,20,25}; //调用sort方法对数组进行升序排序 Arrays.sort(arr); //反向遍历数组,并打印 for (int i = arr.length-1;i>=0;i--){ System.out.print(arr+" "); } }}
输出:
32 25 20 16 15 12
方法二:通过比较器
import java.util.Arrays;import java.util.Comparator;public class ArraysDemo1 { public static void main(String[] args) { //int包装类对象数组,赋值 Integer[] arr = {12,15,32,16,20,25}; //传入引用类型对象arr,用匿名类实现Comparator接口,i1在前则为升序,反正降序 Arrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer i1, Integer i2) { int num = i2 - i1; return num; } }); //打印数组 System.out.println(Arrays.toString(arr)); }}
输出结果:
[32, 25, 20, 16, 15, 12]
|
|