相关疑难解决方法(0)

在java ArrayList中搜索

我正试图找出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;
}
Run Code Online (Sandbox Code Playgroud)

java search arraylist

17
推荐指数
4
解决办法
16万
查看次数

indexOf()将找不到自定义对象类型

以下代码没有给我正确的答案.

class Point {

    int x; int y;
    public Point(int a,int b){
        this.x=a;this.y=b;
    }
}

class A{

    public static void main(String[] args){

        ArrayList<Point> p=new ArrayList<Point>();
        p.add(new Point(3,4));
        p.add(new Point(1,2));
        System.out.println(p.indexOf(1,2));

    }
}
Run Code Online (Sandbox Code Playgroud)

这给了-1;

一般来说,如果给出了arraylist of point,我们怎样才能找到数组中特定点的索引?

java arraylist indexof

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

arraylist ×2

java ×2

indexof ×1

search ×1