【问题标题】:Arrays.sort() of int in decending order [duplicate]int的Arrays.sort()按降序排列[重复]
【发布时间】:2014-05-01 04:40:52
【问题描述】:

简单... 这个程序获取数组中列出的所有数字,并首先找到所有可以除以 for 的数字。其次,它将采用该数组并按降序对数字进行排序

我无法按降序对数组进行排序。 当我使用Arrays.sort(dividend, Collections.reverseOrder());时,Netbeans 一直说“找不到字幕方法”。

import java.util.*;



//This program will take all numbers in the array and First, find all the 
//numbers that are divisible by 4 and then sorts in descending order.

class ArrayFun 

{
 public static void main(String[] args)
{

    //Declaring Variables
    int[] dividend ={100,552,400,111,452,414,600,444,800};



    //Starting While Loop with nested if statement
    for(int i=0; i < dividend.length; i++)
    {     
       //Nested if statement
       if (dividend[i]%4 == 0)
       {
           System.out.println("This number '" +dividend [i]+ "' is divisble by 4.\n");

       }

    }

       //Sorts Array from highest number to lowest
       // This is the area that I am having programs.
       Arrays.sort(dividend, Collections.reverseOrder());

}

}

感谢您的帮助!

【问题讨论】:

标签: java arrays sorting


【解决方案1】:

Collections.reverseOrder() 是一个Comparator&lt;Object&gt;,所以你不能用它对ints 进行排序。通常,您不能创建比较器来对基元进行排序。一种解决方法是将整数放入 Integer[] 并对该数组进行排序(与您尝试对原始数组进行排序的方式相同)。

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2018-08-14
    相关资源
    最近更新 更多