首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在java ArrayList中搜索

在java ArrayList中搜索
EN

Stack Overflow用户
提问于 2009-06-12 06:12:30
回答 7查看 165.7K关注 0票数 17

我正在尝试找出在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;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/985229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档