【发布时间】:2010-11-02 09:11:16
【问题描述】:
我正在尝试找出在ArrayList 中通过 ID 号搜索客户的最佳方式。下面的代码不起作用;编译器告诉我我缺少return 语句。
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 良好实践是总是使用大括号,即使该块只有一个句子,以避免像你这样的问题