2010-04-29 126 views
0

我開始執行這個在數組中執行簡單搜索以進行hw分配的路徑,而不知道我們可以使用ArrayList。我意識到它有一些錯誤,並且認爲在使用ArrayList之前我仍然會嘗試知道我的錯誤是什麼。我基本上有一個類,我可以添加,刪除或從數組中搜索。在沒有ArrayList的Java中返回搜索結果

public class AcmeLoanManager 
{ 
    public void addLoan(Loan h) 
    { 
     int loanId = h.getLoanId(); 
     loanArray[loanId - 1] = h; 
    } 


    public Loan[] getAllLoans() 
    { 
     return loanArray; 
    } 


    public Loan[] findLoans(Person p) 
    { 
     //Loan[] searchedLoanArray = new Loan[10]; // create new array to hold searched values 
     searchedLoanArray = this.getAllLoans(); // fill new array with all values 

     // Looks through only valid array values, and if Person p does not match using Person.equals() 
     // sets that value to null. 
     for (int i = 0; i < searchedLoanArray.length; i++) { 
      if (searchedLoanArray[i] != null) { 
       if (!(searchedLoanArray[i].getClient().equals(p))) { 
        searchedLoanArray[i] = null; 
       } 
      } 
     } 
     return searchedLoanArray; 
    } 

    public void removeLoan(int loanId) 
    { 
     loanArray[loanId - 1] = null; 
    } 

    private Loan[] loanArray = new Loan[10]; 
    private Loan[] searchedLoanArray = new Loan[10]; // separate array to hold values returned from search 
} 

當測試這一點,我想它的工作,但我想我覆蓋我的成員變量我做了搜索之後。我最初認爲我可以在該方法中創建一個新的Loan [],並返回它,但這似乎不起作用。然後我想我可以有兩個數組。一個不會改變,另一個只是搜索值。但我認爲我不理解的東西,如淺與深複製??? ...

+1

您在代碼中遇到了哪些錯誤? – 2010-04-29 02:19:01

回答

1

getAllLoans的返回值覆蓋searledLoanArray引用,這意味着loanArray和searledLoanArray都指向相同的底層數組。嘗試製作searchingLoanArray一個局部變量,然後使用Arrays.copyOf。如果您不想爲家庭作業使用標準函數,請手動創建一個與loanArray大小相同的新Loan數組,然後循環並複製這些值。

1

您的searchloanarray和loanarray指向相同的數組。這樣

private Loan[] searchedLoanArray = new Loan[10] 

什麼也不做,你永遠不會使用新的貸款[10]

這是關鍵,你的問題

searchedLoanArray = this.getAllLoans() 

,只是在loanArray

點searchedLoanArray
0

你可以這樣改寫它:

public Loan[] findLoans(Person p) 
{ 
    Loan[] allLoans = this.getAllLoans(); 
    System.arraycopy(allLoans, searchedLoanArray, 0, 0, allLoans.length); // fill new array with all values 

    // remainder of method the same 

}

但是,因爲它代表,代碼仍然有一些問題:

  1. 貸款的最大數目被固定到所述陣列的大小。當您切換到List<Loan>時,您將避免此問題。
  2. 使用id作爲索引意味着您的ID必須仔細生成。如果ID來自數據庫,您可能會發現該列表嘗試分配大量內存來調整其大小以匹配Id。你會更好地使用地圖,那麼地圖的大小取決於貸款的數量,而不是他們的ID。
  3. 隨着人員和貸款數量的增加,搜索時間也會增加。您可以通過使用Map>將搜索時間縮短爲常數(無論人數多少),從而可以快速查找與該人關聯的貸款。

下面是這些變化的一個版本:

class AcmeLoanManager 
    { 
     public void addLoan(Loan l) 
     { 
     Person client = l.getClient(); 
     List<Loan> loans = clientLoans.get(l); 
     if (loans==null) 
     { 
      loans = new ArrayList(); 
      clientLoans.put(client, loans); 
     } 
     loans.add(l); 
     allLoans.put(l.getLoanId(), l); 
     } 

     public void removeLoan(int loanId) 
     {   
     Loan l = loans.remove(loanId); 
     clientLoans.remove(loan); 
     } 

     public Collection<Loan> getAllLoans() 
     { 
      return loans.values(); 
     } 

     public List<Loan> findLoans(Person p) 
     { 
      List<Loan> loans = clientLoans.get(p); 
      if (loans==null) 
       loans = Collections.emptyList(); 
      return loans; 
     } 

     private Map<Integer,Loan> allLoans = new HashMap<Integer,Loan>(); 
     private Map<Person, List<Loan>> clientLoans = new HashMap<Person,List<Loan>>(); 
    } 

我希望這有助於!