Java实现数组降序的方法

在Java中对一个数组进行升序、降序除了用到冒泡排序法,还可以使用Arrays类自带的sort()方法实现,升序的方法比较常见,降序比升序多一个参数条件:

Collections.reverseOrder()

 

同时需要注意的是降序的时候数组类型只能是包装类型,不能是基本数据类型

 

package _3_5_test;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;

/*数列排序
 * 
 * 
 * */
public class ThirtyThree {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner scanner = new Scanner(System.in);
        
        int n = scanner.nextInt();
        
//        注意这里只能是Integer,Long等包装类,不能是int,long等基本数据类型
        Integer num[] = new Integer[n];
        Long nn[] = new Long[n];
        
        for(int i=0;i<n;i++) {
            num[i] = scanner.nextInt();
        }
        
        Arrays.sort(num,Collections.reverseOrder());
        
        for(int i:num) {
            System.out.print(i+" ");
        }

    }

}

原文地址:https://www.cnblogs.com/lyd447113735/p/12428052.html