我有一堂课叫Person
。它有以下内容attributes
;它有2个属性,ID
, and Telephone
。 1 个人可以拥有多部电话,因此您可能会在下面看到具有多个 ID 的人。
public ArrayList<Person> all(){
p = new ArrayList<Person>();
p.add(new Person(1,266763));
p.add(new Person(1, 358643));
p.add(new Person(2, 4667763));
return p;
}
还有一个类叫做PersonDB
。它将有一个名为的方法,findPersonWithTheTelephoneNumber(int telephone)
.
public void findPersonWithTheTelephoneNumber(int telephone) {
Person pp = new Person();
ArrayList<Person> personList = pp.all();
// Now i want to find the Person object that will match the telephone number of these list of personList.
}
personList 有 3-4 个 Person 对象。我需要搜索 PersonArrayList 并找到与 Person 对象匹配的对象。我怎样才能完成这件事?
注意:我尝试过personList.contains()
。但这行不通。
//...
Person foundPerson = null;
for (Person p : personList){
if (p.getTelephone() == telephone){
foundPerson = p; //or simply return it from there
break;
}
}
为了实施hashCode
and equals
你可以观察本教程.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)