对数组进行排序并获得最大的3个元素

时间:2011-11-05 14:21:32

标签: java arrays sorting

我是初学Java程序员。 我有这个功课:写一个静态方法,在方法的参数是一个Drink数组。该方法返回3个最大的酒精饮料数组元素。

我的问题是:我写的是否有更短的解决方案?

    public static void alcoholMax(AlcoholDrink[] t){
    // sort the AlcoholDrink[] t
    Arrays.sort(t, new Comparator<AlcoholDrink>() { 
        // here I try sorting the elements by their alcohol value
        @Override
        public int compare(AlcoholDrink o1, AlcoholDrink o2) {
            int at1 = (int)o1.getAlcohol(); // get the alcohol value
            int at2 = (int)o2.getAlcohol(); // get the alcohol value
            if(at1>at2)
                return -1;
            else if(at1<at2 )
                return 1;
            return 0;
        }
    });
    // get the 3 largest element of the sorted array
    double t0 = t[0].getAlcohol();
    double t1 = t[1].getAlcohol();
    double t2 = t[2].getAlcohol();
    // check, 1st > 2nd > 3rd, if this not true, return with null reference
    if(t0>t1 && t1>t2)
        System.out.println(t[0] + "\n" + t[1] + "\n" + t[2]);
    else
        System.out.println("null");
}

public static void main(String[] args){
    AlcoholDrink[] t = new AlcoholDrink [8];
    // new AlcoholDrink( String Name, String Stripping, int price, double alcohol)
    // these are hungarian wines :). If somebody curious for the languange
    t[0] = new AlcoholDrink("Kék Portói", "0.75 l", 1200, 20.5);
    t[1] = new AlcoholDrink("Kék Oportó", "0.75 l", 1100, 10.5);
    t[2] = new AlcoholDrink("Tokaji Asszú", "0.75 l ", 1600, 14.5);
    t[3] = new AlcoholDrink("Egri Bikavér", "0.75 l", 1500, 23.5);      
    t[4] = new AlcoholDrink("Egri Leányka", "0.75 l", 1100, 8.5);
    t[5] = new AlcoholDrink("Egri Merlot", "0.75 l", 1700, 18.5);
    t[6] = new AlcoholDrink("Egri Medina", "0.75 l", 900, 16.5);
    t[7] = new AlcoholDrink("Törley Talisman", "0.75 l", 750, 4.5);     

    alcoholMax(t);
    // It is always return with "null"

4 个答案:

答案 0 :(得分:3)

如果您的getAlcohol()方法返回一个double,则不应该将其强制转换为int,这会导致精度损失。此外,您可以自动比较双打而不是自己比较,如下所示:

Arrays.sort(t, new Comparator<AlcoholDrink>() { 
        // here I try sorting the elements by their alcohol value
        @Override
        public int compare(AlcoholDrink o1, AlcoholDrink o2) {
           return o1.getAlcohol().compareTo(o2.getAlcohol());
        }
    });

您还可以让Alcohol类实现Comparable界面,如this示例所示。

最后,如果您想坚持使用自己的代码,可能需要考虑更改compare方法返回的值,如下所示:

 @Override
        public int compare(AlcoholDrink o1, AlcoholDrink o2) {
            int at1 = (int)o1.getAlcohol(); // get the alcohol value
            int at2 = (int)o2.getAlcohol(); // get the alcohol value
            if(at1>at2)
                return -1;
            else if(at1<at2 )
                return 1;
            return 0;
        }

我目前无法测试代码,但您可能会以升序方式对数组进行排序。

答案 1 :(得分:0)

我相信你的数组按升序排序。

在这种情况下,您希望在排序后获得最后3个元素,或者更改比较器。

答案 2 :(得分:0)

在比较器中,如果小于,则返回-1,如果大于,则返回1,并且应该起作用

答案 3 :(得分:0)

First sort the array descending order and get the first three element.

package sortingelementinarray;
public class SortElement 
{
    public static void main(String args[])
    {
        int array[] = {1,6,4,7,2,3};
        int temp;
        for(int j = 0 ; j < array.length; j++)
        {
        for(int i = 0; i < array.length-1; i++)
        {
            if(array[j] > array[i])
            {
                temp = array[j];
                array[j] = array[i];
                array[i] = temp;

            }
        }
        }
        for(int abc : array)
        {
            System.out.println(abc);
        }

    }
}