简体   繁体   English

在ArrayList中搜索特定对象

[英]Search an ArrayList for a particular Object

I have a class called Person . 我有一个名为Person的课程。 It has the following attributes ; 它具有以下attributes ; It has 2 attributes, ID , and Telephone . 它有2个属性, IDTelephone 1 person can have many telephones, so you may see people with multiple ID's below. 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; 
}

There's another class called PersonDB . 还有另一个名为PersonDB的类。 and it will have a method called, findPersonWithTheTelephoneNumber(int telephone) . 它将有一个名为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.


}

The personList, has 3-4 Person objects. personList,有3-4个Person对象。 I need to search the PersonArrayList and find the object that will match the Person object. 我需要搜索PersonArrayList并找到与Person对象匹配的对象。 How can i get this done ? 我怎么能这样做?

Note: i tried personList.contains() . 注意:我试过personList.contains() But this doesn't work. 但这不起作用。

//...
Person foundPerson = null;
for (Person p : personList){
    if (p.getTelephone() == telephone){
         foundPerson = p; //or simply return it from there
         break;
    }
}

For implementing hashCode and equals you can observe this tutorial . 要实现hashCodeequals您可以观察本教程

First of all, why are you not having a List<Integer> for storing all your telephoneNumbers for a particular person. 首先,为什么没有List<Integer>来存储特定人的所有telephoneNumbers That way, you won't have to create a separate Person instance for each telephoneNumber for the same personId , which simply makes no sense. 这样,您就不必为同一personId每个telephoneNumber创建一个单独的Person实例,这根本没有意义。

You can change your attributes of Person class to: - 您可以将Person类的属性更改为: -

private int id;
private List<Integer> telephoneNumbers = new ArrayList<Integer>();

And then have a list of Person, as you are having. 然后有一个Person列表,就像你一样。

To find a Person with a particular telephoneNumber, you need to iterate through your List<Person> . 要查找具有特定telephoneNumber的Person ,您需要遍历List<Person>

for (Person person: personList) {
    if (person.getTelephoneNumbers().contains(telephone)) {
        return person;
    }
}

i tried personList.contains() 我试过personList.contains()

Make sure you override Object.equals() and Object.hashCode() for Person class. 确保为Person类重写Object.equals()Object.hashCode() But you have to have equality check on telephone number assuming telephone number unique. 但是你必须对电话号码进行平等检查,假设电话号码是唯一的。 This would not be a solution but a workaround. 这不是解决方案,而是解决方法。 Use bellum's answer. 使用bellum的答案。 Mark it as correct answer. 将其标记为正确答案。

您需要迭代数组,逐个检查人员的电话号码,当您到达那个时,您需要将其分配给变量。

Use a for loop to iterate through the list. 使用for循环遍历列表。 In the body of the loop, check if the telephone number of the person in the list is the telephone number you're looking for. 在循环体中,检查列表中人员的电话号码是否是您要查找的电话号码。 If yes, then return the person. 如果是,则返回此人。

See The for Statement in Oracle's Java Tutorials. 请参阅Oracle Java教程中for语句

Many solutions, define two persons as equal by their phone number but what if two persons that live in the same house and / or have the same phone number are added to the list? 许多解决方案通过电话号码将两个人定义为相同,但如果将两个住在同一房子和/或具有相同电话号码的人添加到列表中会怎样? Which one is the correct one?. 哪一个是正确的?

Before rushing to find the person, you have to define a way to determine if two persons are, indeed, equal without ambiguous results. 在急于寻找这个人之前,你必须确定一种方法来确定两个人是否确实是平等的而没有模棱两可的结果。 Unless you restrict the creation of persons based on that very phone number by making it unique (you do not clarify this in your question, so I assume there is no such restriction), the result of the search is undefined. 除非您通过使其独一无二来限制基于该电话号码的人员创建(您在问题中没有说明这一点,所以我认为没有这样的限制),搜索的结果是不确定的。

You're using an ArrayList so you can't guarantee even a result by insertion order. 您正在使用ArrayList因此您无法保证插入顺序的结果。

I suggest you base the equality test in a person's ID instead of its phone. 我建议你将相等测试基于一个人的ID而不是手机。 To prevent the modification of id 's just define a getter for it and do not define a setId method at all. 为了防止修改id ,只需为它定义一个getter,并且根本不定义setId方法。 Then, you can redefine equals (and hashcode if you feel like it) based on id . 然后,您可以基于id重新定义equals (如果您愿意,还可以重新定义hashcode )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM