我有一个叫做Person
的类。它有以下attributes
;它有2个属性,ID
和Telephone
。 1人可以有很多电话,所以你可能会看到下面有多个ID的人。在ArrayList中搜索特定的对象
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()
。但这不起作用。
我不喜欢这个解决方案,因为如果他们有相同的电话号码(不管他们的ID),这意味着两个'Person'是相等的。 – Kai
在OP的例子中,我不认为'person.id'可用,所以没有办法进行平等测试。除非电话号码是人们唯一的,并且用于检查平等的唯一人员属性是电话号码。 –
你能告诉我一个例子如何实现这个。我有点新 –