2016-02-20 63 views
0

當前我有這種數據集內排序數組從最低價格到最高價格。Java數組添加算法

Line 1 in the user container need Basic(Special : true) - Ship name: Titanic 

The Price is: 10.0 Capacity: 300.0 

Line 1 in the user container need Basic(Special : true) - Ship name: Ace 

The Price is: 10.0 Capacity: 400.0 

Line 1 in the user container need Basic(Special : true) - Ship name: Adda 

The Price is: 5.0 Capacity: 130.0 

Line 1 in the user container need Basic(Special : true) - Ship name: mutha 

The Price is: 6.0 Capacity: 350.0 

Line 1 in the user container need Basic(Special : true) - Ship name: spade 

The Price is: 10.0 Capacity: 450.0 

結果第一排序後的數組** [5.0,6.0,10.0,10.0,10.0] **每個數據配對與船舶名稱的

我的目標(創建新的字符串數組): [阿達,穆薩,鐵鍬,ACE,泰坦尼克] 阿達是由mutha.For鐵鍬,ACE和鈦的最便宜的後續,它們是由容量,因爲排序它們具有相同的價格 這裏是我的代碼

Map<Ships, Double> sortedMap = sortByComparator(shipsarray); 
      LinkedHashSet<String> uniqueStrings = new LinkedHashSet<String>(); 
      double tempprice=0,tempcap=0; 
      String tempship = null; 

      for (Map.Entry<Ships, Double> entry : sortedMap.entrySet()) {//loop for fill in 
       if((double)entry.getValue() == tempprice){ 
        if(entry.getKey().getAvailableCapacityForType(user.getContainers().get(i).getClass())>tempcap){ 
         System.out.println(uniqueStrings); 
         uniqueStrings.remove(tempship); 
         uniqueStrings.add(entry.getKey().getship().getShipName()); 
         uniqueStrings.add(tempship); 
         System.out.println(uniqueStrings); 
        } 
        else{ 
         System.out.println(uniqueStrings); 
         uniqueStrings.add(entry.getKey().getship().getShipName()); 
        } 
       } 
       else{ 
        uniqueStrings.add(entry.getKey().getship().getShipName()); 
        tempprice = (double)entry.getValue(); 
        tempship = entry.getKey().getship().getShipName(); 
        tempcap = entry.getKey().getAvailableCapacityForType(user.getContainers().get(i).getClass()); 
       } 



      } 

目前的結果是[阿達,穆薩,鐵鍬,泰坦尼克號] 失蹤王牌。

感謝您的幫助

+2

在你的問題中沒有陣列。 –

+0

我建議你學習如何使用調試器來瀏覽你的代碼,看看它在做什麼。另外,你在哪裏使用數組? –

回答

1

你可以嘗試實現自定義的比較,並用它來與Arrays.sort()來獲得想要的順序。就像這樣:

public class Main { 

    public static void main(String[] args) { 

     Ship a = new Ship("A", 5, 100); 
     Ship b = new Ship("B", 6, 400); 
     Ship c = new Ship("C", 6, 300); 
     Ship[] ships = {a,c,b}; 

     System.out.println(Arrays.toString(ships)); 

     Arrays.sort(ships, new Comparator<Ship>() { 
      @Override 
      public int compare(Ship o1, Ship o2) { 
       if (o1.price < o2.price) return -1; 
       if (o1.price > o2.price) return 1; 
       else return o1.capacity > o2.capacity ? -1 : 1; 
      } 
     }); 

     System.out.println(Arrays.toString(ships)); 

    } 

    public static class Ship { 
     public final String name; 
     public final int price; 
     public final int capacity; 
     public Ship(String name, int price, int capacity) { 
      this.name = name; 
      this.price = price; 
      this.capacity = capacity; 
     } 
     @Override 
     public String toString() { 
      return String.format("(%s | price=%d, capacity=%d)", name, price, capacity); 
     } 
    } 

}