我正试图找出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) 以下代码没有给我正确的答案.
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,我们怎样才能找到数组中特定点的索引?