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+" ");
        }

    }

}

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM