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;
}
4

8 回答 8

58

其他人指出了您现有代码中的错误,但我想进一步采取两个步骤。首先,假设您使用的是 Java 1.5+,您可以使用增强的 for 循环获得更高的可读性:

Customer findCustomerByid(int id){    
    for (Customer customer : customers) {
        if (customer.getId() == id) {
            return customer;
        }
    }
    return null; 
}

这也消除了null循环前返回的微优化——我怀疑你会从中得到任何好处,而且它是更多的代码。同样,我删除了exists标志:一旦您知道答案就返回使代码更简单。

请注意,在您的原始代码中,我认为您有一个错误。发现 index 处的客户i具有正确的 ID,然后您返回了 index 处的客户id- 我怀疑这是否真的是您想要的。

其次,如果您要通过 ID 进行大量查找,您是否考虑过将您的客户放入Map<Integer, Customer>?

于 2009-06-12T06:28:14.077 回答
18

编译器正在抱怨,因为您当前在 for 循环中有“if(exist)”块。它需要在它之外。

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);
}

话虽如此,有更好的方法来执行此搜索。就我个人而言,如果我使用的是 ArrayList,我的解决方案将类似于 Jon Skeet 发布的解决方案。

于 2009-06-12T06:18:02.697 回答
16

就个人而言,我现在很少自己编写循环,因为我可以摆脱它......我使用 Jakarta commons libs:

Customer findCustomerByid(final int id){
    return (Customer) CollectionUtils.find(customers, new Predicate() {
        public boolean evaluate(Object arg0) {
            return ((Customer) arg0).getId()==id;
        }
    });
}

耶!我省了一行!

于 2009-06-12T09:06:48.127 回答
11
Customer findCustomerByid(int id){
    for (int i=0; i<this.customers.size(); i++) {
        Customer customer = this.customers.get(i);
        if (customer.getId() == id){
             return customer;
        }
    }
    return null; // no Customer found with this ID; maybe throw an exception
}
于 2009-06-12T06:18:30.043 回答
2

您缺少 return 语句,因为如果您的列表大小为 0,则 for 循环将永远不会执行,因此 if 将永远不会运行,因此您将永远不会返回。

将 if 语句移出循环。

于 2009-06-12T06:18:39.623 回答
2

即使这个话题很老,我也想补充一点。如果你equals为你的类覆盖,所以它比较你的getId,你可以使用:

customer = new Customer(id);
customers.get(customers.indexOf(customer));

当然,您必须检查是否存在IndexOutOfBounds-Exception,它可能会被转换为空指针或 custom CustomerNotFoundException

于 2012-10-11T07:59:24.207 回答
1

在 Java 8 中:

Customer findCustomerByid(int id) {
    return this.customers.stream()
        .filter(customer -> customer.getId().equals(id))
        .findFirst().get();
}

将返回类型更改为Optional<Customer>.

于 2015-10-20T17:48:03.370 回答
0

我做了一些接近于此的事情,编译器看到您的 return 语句在 If() 语句中。如果您希望解决此错误,只需在 If 语句之前创建一个名为 customerId 的新局部变量,然后在 if 语句内分配一个值。在 if 语句之后,调用您的 return 语句,并返回 cstomerId。像这样:

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;
        }

        int customerId;

        if(exist) {
            customerId = this.customers.get(id);
        } else {
            customerId = this.customers.get(id);
        }
    }
    return customerId;
}
于 2013-08-21T21:34:43.027 回答