2011-11-05 70 views
0

我是初學java程序員。 我有這樣的作業:寫一個靜態方法,在方法的參數中是一個Drink數組。該方法返回3個最大的酒精飲料數組元素。對數組進行排序並獲得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" 
+0

好吧,我知道......我必須要改變的,如果返回值:) :)案件+1 – blaces

回答

3

如果您getAlcohol()方法返回一個雙的話,你不應該將其轉換爲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例子。

最後,如果你想堅持自己的代碼,你可能要考慮作出改變,以通過比較法,像這樣的返回值:

@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; 
     } 

我不能在測試代碼那一刻,但是你可能會以一種上升的方式對數組進行排序。

+0

爲雙 – r0ast3d

+0

是雙:)謝謝! – blaces

+0

是的,我在Alcohol類中實現了Comparable接口,但prof說:我們只能實現o1.name.compareto(o2.name)邏輯,並且不能使用o1.getAlcohol()。compareTo(o2。 getAlcohol()); – blaces

0

您的數組按升序排序我相信。

在這種情況下,您想要在排序後得到最後3個元素,或者更改比較器。

0

在你的比較,返回-1如果小於1,如果是大於,並應工作

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

    } 
} 
相關問題