2015-10-03 66 views
0

我不認爲我會有那麼多的麻煩,因爲我已經用常規數組的強力方法做到這一點。我試圖使用Collections.frequency將來自用戶的重複條目分組在一起,並顯示每個條目發生的次數(如果不止一次);但是,輸出只是在輸入時纔打印。我有兩個類,我將在下面顯示:ArrayList不打印重複項(Java)

public class Flower { 

private String name; 
private String color; 
private String smell; 
private String hasThorns; 

public Flower(String name, String color, String smell, String hasThorns) { 
    this.name = name; 
    this.color = color; 
    this.smell = smell; 
    this.hasThorns = hasThorns; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public void setColor(String color) { 
    this.color = color; 
} 
public void setSmell(String smell) { 
    this.smell = smell; 
} 
public void setThorns(String hasThorns) { 
    this.hasThorns = hasThorns; 
} 
public String getName() { 
    return name; 
} 
public String getColor() { 
    return color; 
} 
public String getSmell() { 
    return smell; 
} 
public String getThorns() { 
    return hasThorns; 
} 
public String toString() { 
    return "Name: " + this.name + ", Color: " + this.color + ", Scent? " + this.smell + ", Thorns? " + this.hasThorns; 
} 
}//end Flower class 



    import java.util.*; 

public class Assignment3 { 
String name = new String(); 
String color = new String(); 
String smell = new String(); 
String hasThorns = new String(); 

//ArrayList<Flower> flowerPack = new ArrayList<Flower>(); 
boolean found = false; 
public static void main(String[] args) { 
    new Assignment3(); 
} 

// This will act as our program switchboard 
public Assignment3() { 
    Scanner input = new Scanner(System.in); 
    ArrayList<Flower> flowerPack = new ArrayList<Flower>(); 

    System.out.println("Welcome to my flower pack interface."); 
    System.out.println("Please select a number from the options below"); 
    System.out.println(""); 

    while (true) { 
     // Give the user a list of their options 
     System.out.println("1: Add an item to the pack."); 
     System.out.println("2: Remove an item from the pack."); 
     System.out.println("3: Search for a flower."); 
     System.out.println("4: Display the flowers in the pack."); 
     System.out.println("5: Filter flower pack by incomplete name"); 
     System.out.println("0: Exit the flower pack interface."); 

     // Get the user input 
     int userChoice = input.nextInt(); 

     switch (userChoice) { 
      case 1: 
       addFlower(flowerPack); 
       break; 
      case 2: 
       removeFlower(flowerPack); 
       break; 
      case 3: 
       searchFlowers(flowerPack); 
       break; 
      case 4: 
       displayFlowers(flowerPack); 
       break; 
      case 5: 
       filterFlowers(flowerPack); 
       break; 
      case 0: 
       exitInterface(); 
       break; 
      default: 
       System.out.println("Invalid entry. \nPlease choose between 1-5, or 0: "); 
       break; 
     } 
    } 
} 
private void addFlower(ArrayList<Flower> flowerPack) { 
    // TODO: Add a flower that is specified by the user 
    Flower newFlower = new Flower(name, color, smell, hasThorns); 

    Scanner input = new Scanner(System.in); 

    if(flowerPack.size() < 25) 
    { 
     System.out.println("Enter the name of the flower you wish to add: "); 
     newFlower.setName(input.nextLine()); 
     System.out.println("Enter the color of the flower: "); 
     newFlower.setColor(input.nextLine()); 
     System.out.println("Does the flower have a scent? Yes or No: "); 
     newFlower.setSmell(input.nextLine()); 
     System.out.println("Does the flower have thorns? Yes or No: "); 
     newFlower.setThorns(input.nextLine()); 

     flowerPack.add(newFlower); 
    } 
    else 
    { 
     System.out.println("You may only hold 25 flowers in your flower pack. Please remove at least one before adding another."); 
    } 
} 
private void removeFlower(ArrayList<Flower> flowerPack) { 
    // TODO: Remove a flower that is specified by the user 
    Scanner input = new Scanner(System.in); 

    System.out.println("Enter the name of the flower you want to remove: "); 
    String deleteName = input.nextLine(); 
    System.out.println("Enter the color of the flower you want to remove: "); 
    String deleteColor = input.nextLine(); 
    System.out.println("Is this a flower with a scent? Yes or No: "); 
    String deleteSmell = input.nextLine(); 
    System.out.println("Is this a flower with thorns? Yes or No: "); 
    String deleteThorns = input.nextLine(); 

    for(int i = 0; i < flowerPack.size(); i++) { 
     if(flowerPack.get(i).getName().equals(deleteName) && flowerPack.get(i).getColor().equals(deleteColor) && flowerPack.get(i).getSmell().equals(deleteSmell) && flowerPack.get(i).getThorns().equals(deleteThorns)) 
     { 
      flowerPack.remove(i); 
      found = true; 
      break; 
     } 
     if(found) 
     { 
      System.out.println("That flower was successfully removed from your inventory."); 
     } 
     else 
     { 
      System.out.println("That flower was not found in your inventory."); 
     } 
    } 
} 
private void searchFlowers(ArrayList<Flower> flowerPack) { 
    // TODO: Search for a user specified flower 

} 

這是我遇到問題的地方。我還沒有開始其他方法(搜索,過濾器),因爲我希望它在繼續前正確顯示。

private void displayFlowers(ArrayList<Flower> flowerPack) { 
    // TODO: Display flowers using any technique you like 

    for(Flower flower : flowerPack) { 
     int duplicates = Collections.frequency(flowerPack, flower); 
     System.out.println(flower + " - " + duplicates); 
    } 
} 

private void filterFlowers (ArrayList<Flower> flowerPack) { 
    // TODO Filter flower results 

} 
private void exitInterface() { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Are you sure you want to exit the flower pack interface? Y or N: "); 
    while(true) { 
     String answer = input.next(); 
     if(!"Y".equalsIgnoreCase(answer) && !"N".equalsIgnoreCase(answer)) 
     { 
      System.out.println("Please enter Y or N (not case-sensitive): "); 
     } 
     if("Y".equalsIgnoreCase(answer)) 
     { 
      System.out.println("Thank you for using the flower pack interface. See ya later!"); 
      System.exit(0); 
     } 
     if("N".equalsIgnoreCase(answer)) 
     { 
      break; 
     } 
    } 
} 
} 

我已經看到了地圖/ HashSets的例子,但我與利用他們在這一點上這些概念太陌生。

這是輸出,這是不正確的。有人能給我一個暗示我出錯的地方嗎?

Name: rose, Color: red, Scent? yes, Thorns? yes - 1 
    Name: rose, Color: red, Scent? yes, Thorns? yes - 1 
    Name: rose, Color: pink, Scent? yes, Thorns? no - 1 
    Name: daffodil, Color: yellow, Scent? yes, Thorns? no - 1 

正如你可以看到,前兩個項目應爲一個,但-2:

像:

Name: rose, Color: red, Scent? yes, Thorns? yes - 2 
+0

請閱讀[如何創建一個最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) – Andreas

+0

我不知道如果有人想運行它並查看結果自己。我看到有人抱怨說某人只包含了一小段代碼,而評論則是「你的其他代碼在哪裏?我不能用這個等等等等」。 @Andreas – IRGeekSauce

+1

是的,這是「最小」和「完整」之間的隱含衝突。訣竅是用最少的代碼展示問題,有時候你實際上最終會自己找到錯誤。請記住,您只會發佈一次該問題,但很多人會閱讀這個問題,所以任何簡化/簡化都可以幫助所有人。另外,改善代碼的縮進可以幫助人們閱讀它。 – Andreas

回答

0

Collections.frequency使用Flower.equals,以決定是否兩朵花都是一樣的或不。由於您尚未覆蓋equals的默認實現,因此Flower的所有實例都是唯一的。

您可以使用IDE生成應該很好地工作, 然後頻率計數將成爲正確的equals(和hashCode)實施, 爲邏輯上等於花將被檢測到。

例如,通過產生的IntelliJ,在Flower補充一點:

@Override 
public boolean equals(Object o) { 
    if (this == o) { 
     return true; 
    } 
    if (o == null || getClass() != o.getClass()) { 
     return false; 
    } 

    Flower flower = (Flower) o; 

    if (color != null ? !color.equals(flower.color) : flower.color != null) { 
     return false; 
    } 
    if (hasThorns != null ? !hasThorns.equals(flower.hasThorns) : flower.hasThorns != null) { 
     return false; 
    } 
    if (name != null ? !name.equals(flower.name) : flower.name != null) { 
     return false; 
    } 
    if (smell != null ? !smell.equals(flower.smell) : flower.smell != null) { 
     return false; 
    } 

    return true; 
} 

@Override 
public int hashCode() { 
    int result = name != null ? name.hashCode() : 0; 
    result = 31 * result + (color != null ? color.hashCode() : 0); 
    result = 31 * result + (smell != null ? smell.hashCode() : 0); 
    result = 31 * result + (hasThorns != null ? hasThorns.hashCode() : 0); 
    return result; 
} 
+0

我發現通過在我的IntelliJ任務欄中單擊「代碼」並單擊覆蓋方法。雖然我對此一無所知,但我可以更多地瞭解它。這似乎遠遠超出我們迄今爲止所瞭解的情況。我希望我們被教導「如果」情況。我們可以選擇顯示數組列表,但是我們想要使用與常規數組相同的輸出。 – IRGeekSauce

+0

嘿,什麼是「結果= 31」和「?」在hashCode()方法? – IRGeekSauce

+0

看到這篇文章:http://stackoverflow.com/a/27609/641955(和關於equals和hashCode的文檔) – janos

1

你花類需要一個equals和hashCode方法,否則的Java不能告訴如果兩個花對象是相同的。