我试图找出通过其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