简体   繁体   English

使用String作为参数在ArrayList上进行线性搜索

[英]Linear search over an ArrayList using a String as a parameter

I am trying to search for a String contained in an ArrayList in my method, findContactsByName, using a for-each loop, if the String is found in the Contact then the Contact is returned and if the String is not found then null is returned. 我正在尝试使用for-each循环在我的方法findArraysByName中搜索包含在ArrayList中的字符串,如果在Contact中找到了String,则返回了Contact,如果没有找到String,则返回了null。 However when testing my code null is returned all the time, here is my code: 但是,在测试我的代码始终为null时,这是我的代码:

import java.util.*;

public class Phonebook {
    private String owner;
    private ArrayList<Contact> contacts = new ArrayList<Contact>();

    public void addContact(Contact contact) {
        contacts.add(contact);
    }

    public void show() {
        System.out.println(owner + "'s phonebook");
        for (Contact contact : contacts) {
            System.out.println(contact);
        }
    }

    public Contact findContactByName(String name) {
        for (Contact contact : contacts) {
            if (contacts.contains(name)) {
               return contact;
           }
       }
       return null;
    }
}

Your findContactByName method isn't correct. 您的findContactByName方法不正确。 You are looping through the ArrayList, but each loop you are checking the array list by using ArrayList.contains , which is comparing the objects in the list itself with name . 您正在遍历ArrayList,但是每个循环都通过使用ArrayList.contains检查数组列表,该列表将列表本身中的对象与name进行比较。

The correct way to do it would be: 正确的方法是:

public Contact findContactByName(String name) {
    for (Contact contact : contacts) {
        if (contact.getName().equals(name)) {
            return contact;
        }
   }
   return null;
}

That is assuming that the Contact object has a method called getName . 假设Contact对象具有一个名为getName的方法。 Notice how in each loop i'm using contact from the for loop for (Contact contact : contacts) instead of the ArrayList itself. 请注意,在每个循环我使用contact从for循环(Contact contact : contacts) ,而不是ArrayList的本身。

public Contact findContactByName(String name) {
    for (Contact Contact : contacts) {
        if (contacts.contains(name)) {
            return Contact;
        }
   }
   return null;
}

This function is the root cause of your problem. 此功能是问题的根本原因。 Your condition in the if block reads: contacts.contains(name) where contacts is the entire list of contacts and you are looking in there for a string object. if块中的条件为: contacts.contains(name) ,其中contacts是整个联系人列表,并且您正在其中查找字符串对象。 That can't work. 那行不通。 You want to find out if your contacts contain a contact with a specific name. 您想确定您的联系人是否包含具有特定名称的联系人。 So something like: contact.getName().equals(name) . 所以像这样: contact.getName().equals(name)

So your function should be something like this: 因此,您的函数应如下所示:

public Contact findContactByName(String name) {
    for (Contact contact : contacts) {
        if (contact.getName().equals(name)) {
            return contact;
        }
   }
   return null;
}

Example for using contains is 使用contains的示例是

List<String> fruitsList = new ArrayList<>();
fruitsList.add("Apple");
fruitsList.add("Mango");
fruitsList.add("Grapes");

if(fruitsList.contains("Grapes")) {
S.o.p("Found Grapes");
}

but for your case you need to find a specific element from your dto and return it back if specific name is found, so your usecase is: 但是对于您的情况,您需要从dto中找到一个特定的元素,如果找到了特定的名称,则将其返回,因此您的用例是:

public Contact findContactByName(String name) {
    for (Contact contact : contacts) {
        if ((null!=contact.getName()) && contact.getName().equals(name)) {
            return contact;
        }
   }
   return null;
}

Assuming your Contact class has name variable. 假设您的Contact类具有名称变量。 Hope you understood it. 希望你能理解。

Unless using ArrayList is specifically required, I suggest you better replace it by a HashMap<String, Contact> , so you can avoid coding the search algorithm from the scratch simply by relying on the Map.get(key) method. 除非特别需要使用ArrayList ,否则我建议您最好HashMap<String, Contact>替换它 ,这样您就可以避免仅仅依靠Map.get(key)方法从头开始编写搜索算法。 Maps are intended to search values by some key (indexing): 映射旨在通过某些键(索引)来搜索值:

public class Phonebook {

    private Map<String, Contact> contacts = new HashMap<String, Contact>();

    public void addContact(Contact Contact) {
        contacts.put(contact.getName(), contact);
    }

    public Contact findContactByName(String name) {
        return contacts.get(name);
    }
}

Also, the search becomes then indexed and thus, faster. 而且,搜索随后被索引 ,因此速度更快。

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

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