2016-02-20 2 views
0

actuellement j'ai cet ensemble de données à l'intérieur d'un tableau trié du prix le plus bas au prix le plus élevé.Java algorithme d'ajout de tableau

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 

Le résultat du premier réseau ** Sorted [5.0,6.0,10.0,10.0,10.0] ** chaque donnée est jumelé avec le nom du navire.

Mon objectif (créer un nouveau tableau de chaînes): [Adda, Mutha, pelle, as, Titanic] est le Adda suite moins cher par chat mutha.For, as et titanesque, ils sont classés par la capacité depuis ils ont le même prix Voici mon code

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



      } 

actuellement le résultat est [Adda, mutha, pelle, Titanic] manquant as.

Merci de nous aider

+2

Il n'y a pas de tableaux dans votre question. –

+0

Je vous suggère d'apprendre à utiliser un débogueur pour parcourir votre code pour voir ce qu'il fait. Aussi, où utilisez-vous un tableau? –

Répondre

1

Vous pouvez essayer de mettre en place un comparateur personnalisé et l'utiliser avec Arrays.sort() pour obtenir la commande voulue. Quelque chose comme ceci:

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

}