简单来说..。这个程序获取数组中列出的所有数字,并首先查找可以被整除的所有数字。其次,它将获取该数组并按降序对数字进行排序
我无法按降序对数组进行排序。当我使用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());
}
}谢谢你的帮助!
发布于 2014-03-24 08:19:59
Collections.reverseOrder()是一个Comparator<Object>,所以你不能用它对int进行排序。一般来说,您不能创建一个比较器来对基元进行排序。一种解决办法是将整数放入一个Integer[]中并对该数组进行排序(与您尝试对原始数组进行排序的方式相同)。
https://stackoverflow.com/questions/22598989
复制相似问题