2013-03-10 37 views
0

好吧,我試圖正確地說出問題,以便獲得我需要的幫助。我得到的是一個簡單的錢包/硬幣程序,它將字符串數組與另一個字符串數組進行比較。原諒長循環/嵌套循環/邏輯,因爲在這個任務中,我不能使用java數組和集合類中的方法。 這是一個班級作業,所以請解釋過程,而不僅僅是回答。 (理論):我在想,我對2個轉換數組的比較是問題的原因,但我找不出一種方法來比較數組列表中的每個元素與另一個數組中的每個元素名單。轉換後的數組可以與另一個轉換後的數組進行比較嗎?

Purse.class:

import java.util.ArrayList; 

/** 
* A purse holds a collection of coins. 
*/ 
public class Purse 
{ 
    private ArrayList<String> coins; 

    /** 
    * Constructs an empty purse. 
    */ 
    public Purse() 
    { 
     coins = new ArrayList<String>(); 
    } 

    /** 
    * Add a coin to the purse. 
    * 
    * @param coinName 
    *   the coin to add 
    */ 
    public void addCoin(String coinName) 
    { 
     coins.add(coinName); 
    } 

    /** 
    * Returns a string describing the object. 
    * 
    * @return a string in the format "Purse[coinName1,coinName2,...]" 
    */ 
    public String toString() 
    { 
     String coinName1 = "Quarter"; 
     String coinName2 = "Dime"; 
     String coinName3 = "Nickel"; 
     String coinName4 = "Penny"; 

     String str = "Actual:" 
       + "Purse[" 
       + (coinName1 + "," + coinName2 + "," + coinName3 + "," + coinName2) 
       + "]"; 

     return str; 
    } 

    /** 
    * Determines if a purse has the same coins in the same or different order 
    * as another purse. 
    * 
    * @param other 
    *   the other purse 
    * @return true if the two purses have the same coins in the same or 
    *   different order, false otherwise 
    */ 
    public boolean sameCoins(Purse other) 
    { 
     if (this.coins.size() != other.coins.size()) 
     { 
      System.out.println("1"); 
      return false; 
     } 


     int matched = 0; 
     for (int i = 0; i < this.coins.size(); i++) 
     { 
      for (int j = 0; j < other.coins.size(); j++) 
      { 
       if (this.coins.toArray() == other.coins.toArray()) 
       { 
        matched++; 
        System.out.println("2"); 
        System.out.println(this.coins.toArray()); 
        System.out.println(other.coins.toArray()); 
        break; 
       } 
      } 
     } 
     return matched == this.coins.size(); 

    } 

} 

PurseTester.class:

/** 
* This class tests the Purse class. 
*/ 
public class PurseTester 
{ 
    public static void main(String[] args) 
    { 
     Purse p = new Purse(); 
     p.addCoin("Quarter"); 
     p.addCoin("Dime"); 
     p.addCoin("Nickel"); 
     p.addCoin("Dime"); 

     System.out.println(p.toString()); 
     System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]"); 

     Purse a = new Purse(); 
     a.addCoin("Quarter"); 
     a.addCoin("Dime"); 
     a.addCoin("Nickel"); 
     a.addCoin("Dime"); 

     Purse b = new Purse(); 
     b.addCoin("Nickel"); 
     b.addCoin("Dime"); 
     b.addCoin("Dime"); 
     b.addCoin("Quarter"); 

     System.out.println(a.sameCoins(b)); 
     System.out.println("Expected: true"); 

     Purse c = new Purse(); 
     c.addCoin("Quarter"); 
     c.addCoin("Penny"); 
     c.addCoin("Nickel"); 
     c.addCoin("Dime"); 

     Purse d = new Purse(); 
     d.addCoin("Nickel"); 
     d.addCoin("Dime"); 
     d.addCoin("Dime"); 
     d.addCoin("Quarter"); 

     System.out.println(c.sameCoins(d)); 
     System.out.println("Expected: false"); 

    } 
} 

輸出是:

Actual:Purse[Quarter,Dime,Nickel,Dime] 
Expected: Purse[Quarter,Dime,Nickel,Dime] 
false 
Expected: true 
false 
Expected: false 

預期輸出:

Actual:Purse[Quarter,Dime,Nickel,Dime] 
Expected: Purse[Quarter,Dime,Nickel,Dime] 
true 
Expected: true 
false 
Expected: false 

回答

1

您的循環從不查看List s,您只需在兩個陣列上重複檢查==即可。您需要比較的元素,如:

for (int i = 0; i < this.coins.size(); i++) 
    { 
     if (!this.coins.get(i).equals(other.coins.get(i))) 
     { 
      return false; 
     } 
    } 

但是,這是假設你想要相同的順序進行比較。如果您需要比較忽略順序,則需要循環訪問另一個數組,如果找到該元素,請移除該元素,否則請使用return false

final List<String> copy = new ArrayList<String>(other.coins); 
    outerLoop: 
    for (final String mine : coins) 
    { 
     final Iterator<String> otherIter = copy.iterator(); 
     while (otherIter.hasNext()) 
     { 
      if (mine.equals(otherIter.next())) 
      { 
       otherIter.remove(); 
       continue outerLoop; 
      } 
     } 
     return false; 
    } 

這種方法顯然是效率非常低,如果排序的List第一個,然後用這將是最佳的第一種方法。但鑑於你不能使用Array方法,我假設你不能使用TreeSetCollections.sort()

+0

沒有** remove的迭代器方法**基本上和我所認爲的一樣(我相信)OP正在嘗試使用double-for-loop來做**。值得注意的是,如果第一個數組中有兩個相同的項目,並且第二個數據項中只有一個項目,那麼這個(不帶'remove')可以將2個數組分類爲相等,因爲兩個數組都會匹配到一個項目。 – Dukeling 2013-03-10 22:51:15

+0

是的,這個工程。我只是指出,我認爲OP可能一直試圖做的事情不會起作用,這與此類似,並且由於「移除」而沒有這個問題。也許有人會覺得有用。 – Dukeling 2013-03-10 22:56:35

1
this.coins.toArray() == other.coins.toArray() 

此代碼總是返回false,因爲它不比較兩個陣列的內容,它測試兩個表達式是否引用相同的陣列(即,修改一個將修改以外)。

基本上,你需要計算每個列表中每種硬幣的數量,然後比較結果。如果事先知道可能種類的硬幣,很容易:每種硬幣只有兩個變量(一個用於這個錢包,另一個用於另一個錢包)。如果可能的硬幣扭曲不知道,你將不得不使用Map

+0

「是相同的」有點不清楚「,參考/指向完全相同的對象(主要是通過在'='-operator)的某個點實際設置它們相等來實現」)會更好。 – Dukeling 2013-03-10 22:43:01

+0

所以基本上你所說的是我必須採取字符串值並將它們設置爲一個數字值。然後讓每個數組的總數與另一個數組的總數相比較? – SkyVar 2013-03-10 22:53:33

+1

完全沒有。我的意思是看一個數組,計算它包含的四分之一數量,然後查看另一個數組,計算它包含的四分之一數量,然後比較兩個結果。如果它們不一樣,如果它們返回「假」,那麼繼續下一種硬幣。 – 2013-03-10 22:57:16

相關問題