我試圖找出通過其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;
}
其中一個主要的Java良好實踐是總是使用大括號,即使該塊只有一個句子,以避免類似於你的問題 – 2009-06-12 06:24:01