2009-06-12 92 views
15

我試圖找出通過其Id編號搜索ArrayList中的客戶的最佳方法。下面的代碼不起作用;編譯器告訴我,我錯過了return聲明。在java中搜索ArrayList

Customer findCustomerByid(int id){ 
    boolean exist=false; 

    if(this.customers.isEmpty()) { 
     return null; 
    } 

    for(int i=0;i<this.customers.size();i++) { 
     if(this.customers.get(i).getId() == id) { 
      exist=true; 
      break; 
     } 

     if(exist) { 
      return this.customers.get(id); 
     } else { 
      return this.customers.get(id); 
     } 
    } 

} 

//the customer class is something like that 
public class Customer { 
    //attributes 
    int id; 
    int tel; 
    String fname; 
    String lname; 
    String resgistrationDate; 
} 
+12

其中一個主要的Java良好實踐是總是使用大括號,即使該塊只有一個句子,以避免類似於你的問題 – 2009-06-12 06:24:01

回答

16

編譯器抱怨,因爲你目前在for循環中有'if(exist)'塊。它需要在它之外。

for(int i=0;i<this.customers.size();i++){ 
     if(this.customers.get(i).getId() == id){ 
      exist=true; 
      break; 
     } 
} 

if(exist) { 
    return this.customers.get(id); 
} else { 
    return this.customers.get(id); 
} 

這就是說,有更好的方法來執行此搜索。就個人而言,如果我使用ArrayList,我的解決方案看起來就像Jon Skeet發佈的解決方案。

+0

是的,我對其進行了重新格式化,現在更明顯 – 2009-06-12 06:19:35

+0

我強烈懷疑代碼仍然破碎(注意在customers.get最後的參數)。這兩個分支都有相同代碼的「if/else」事實也是非常可疑的! – 2009-06-12 06:32:12

+0

我同意,Jon。此外,還有更好的方法來進行搜索(就像其他海報所指出的那樣)。 – 2009-06-12 06:35:41

10
Customer findCustomerByid(int id){ 
    for (int i=0; i<this.customers.size(); i++) { 
     Customer customer = this.customers.get(i); 
     if (customer.getId() == id){ 
      return customer; 
     } 
    } 
    return null; // no Customer found with this ID; maybe throw an exception 
} 
2

你缺少return語句,因爲如果您的列表大小爲0,for循環將不會執行,因此,如果將永遠不會運行,因此你將永遠不會返回。

將if語句移出循環。

48

其他人已經指出了你現有的代碼中的錯誤,但我想進一步採取兩個步驟。首先,假設你使用Java 1.5+,您可以使用取得更大的可讀性增強的for循環

Customer findCustomerByid(int id){  
    for (Customer customer : customers) { 
     if (customer.getId() == id) { 
      return customer; 
     } 
    } 
    return null; 
} 

這也去掉循環之前返回null的微型優化 - 我懷疑你」我們可以從中獲得任何好處,而且它是更多的代碼。同樣,我已經刪除了exists標誌:只要您知道答案使代碼更簡單,就會返回。

請注意,在您的原始代碼我認爲你有一個錯誤。在發現指數爲i的顧客擁有正確的身份證明後,您就以id索引返回了顧客 - 我懷疑這確實是你的意圖。其次,如果你打算通過ID做很多查詢,你有沒有考慮過把你的客戶放到Map<Integer, Customer>

16

個人而言,我很少寫自己的循環現在,當我可以逃脫它...我用的Jakarta Commons庫:

Customer findCustomerByid(final int id){ 
    return (Customer) CollectionUtils.find(customers, new Predicate() { 
     public boolean evaluate(Object arg0) { 
      return ((Customer) arg0).getId()==id; 
     } 
    }); 
} 

耶!我保存了一行!

2

即使該主題相當老,我想添加一些東西。 如果覆蓋equals爲你的類,所以它會比較您getId,你可以使用:

customer = new Customer(id); 
customers.get(customers.indexOf(customer)); 

當然,你必須檢查的IndexOutOfBounds -Exception,這oculd被轉換成一個空指針或自定義CustomerNotFoundException

0

我做了一些與之相近的事情,編譯器看到你的return語句在If()語句中。如果你想解決這個錯誤,只需在If語句之前創建一個名爲customerId的新局部變量,然後在if語句內部分配一個值。在if語句之後,調用你的return語句,並返回cstomerId。 像這樣:

Customer findCustomerByid(int id){ 
boolean exist=false; 

if(this.customers.isEmpty()) { 
    return null; 
} 

for(int i=0;i<this.customers.size();i++) { 
    if(this.customers.get(i).getId() == id) { 
     exist=true; 
     break; 
    } 

    int customerId; 

    if(exist) { 
     customerId = this.customers.get(id); 
    } else { 
     customerId = this.customers.get(id); 
    } 
} 

return customerId;

}

1

在Java 8:

Customer findCustomerByid(int id) { 
    return this.customers.stream() 
     .filter(customer -> customer.getId().equals(id)) 
     .findFirst().get(); 
} 

它也可能會更好的返回類型更改爲Optional<Customer>