Sto cercando di capire il modo migliore per cercare un cliente in un ArrayList
con il suo numero di identificazione. Il codice qui sotto non funziona; il compilatore mi dice che mi manca un'istruzione return
.cerca in 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;
}
Uno dei principali Java Buone Pratiche è di usare sempre le parentesi, anche se il blocco ha una sola frase per evitare problemi come il vostro –