简体   繁体   English

将IndexOf与arrayList中的customObject一起使用

[英]Using IndexOf with a customObject in an arrayList

I'm wanting to be able to return the position of an object using the indexOf method, but only want to pass the name of the contact to search for this, is there any way for this to be done? 我希望能够使用indexOf方法返回对象的位置,但只想传递联系人的名称来搜索它,有什么办法可以做到这一点吗?

I currently have this method: 我目前有这种方法:

private static ArrayList<Contacts> contactList = new ArrayList<Contacts>();

public class Contacts {
private String name;
private String number;


public Contacts(String name, String number) {
    this.name = name;
    this.number = number;
}

public String getName() {
    return name;
}

public String getNumber() {
    return number;
}

public void setName(String name) {
    this.name = name;
}

public void setNumber(String number) {
    this.number = number;
}



public int findItem(String name) {

    return contactList.indexOf(name);
}

Heres a function that will achieve this without going through the whole list, I think the complexity is less than O(n): 下面是一个功能,无需通过整个列表即可实现此功能,我认为复杂度低于O(n):

public int findItem(String name)
    {
        int max = contactList.size();

        //you might have to subtract this by one 
        //I'm not sure off the top
        int descCnt = max;


        for(int cnt = 0; cnt <= max/2; cnt++)
        {
            if(contactList.get(cnt).getName().equals(name)) return cnt;
            if(contactList.get(descCnt).getName().equals(name)) return descCnt;
            --descCnt;
        }

    }

If you are doing a lot of lookups of Contacts by name, you can put the instances into a Map<String, Contacts> . 如果按名称对Contacts进行大量查找,则可以将实例放入Map<String, Contacts> The specific type of Map depends upon your requirements; 具体的Map类型取决于您的要求; a HashMap might suffice. HashMap可能就足够了。

Instead of contactList.add(contacts) , you can use: 您可以使用以下代码而不是contactList.add(contacts)

contactMap.put(contacts.getName(), contacts);

and then look up an item in the map using: 然后使用以下方法在地图中查找项目:

contactMap.get(someName);

This will be faster to do the lookups than scanning through a list each time: each lookup will be O(1) for a HashMap , compared to O(n) for a list. 执行查找比每次扫描列表更快:每个查找对于HashMap将是O(1) ,而对于列表则是O(n) However, it uses more memory. 但是,它使用更多内存。


Incidentally, your Contacts class looks like it represents a single contact, so it should be named as a singular: Contact . 顺便提一下,您的Contacts类看起来像是一个单独的联系人,因此它应该被命名为单数: Contact

Also, your find method is currently declared as an instance method: 此外,您的find方法当前被声明为实例方法:

public int findItem(String name) {

meaning you actually need an instance of Contacts to find another instance of Contacts . 这意味着你实际上需要一个Contacts实例来查找另一个Contacts实例。 Instead, declare it static : 相反,声明它是static

public static int findItem(String name) {

then you can invoke it without an instance: 然后你可以在没有实例的情况下调用它:

Contacts found = Contacts.find("name");

What you ask is not in the contract of List#indexOf(Object) , so no, you should not try to make the list work that way. 你问的不在List#indexOf(Object)的合同中,所以不,你不应该试着让列表以这种方式工作。

Instead, you can write your own method that will accomplish what you want relatively easily. 相反,您可以编写自己的方法,以便相对轻松地完成您想要的任务。 Simply iterate over your list and find the Contact that matches the specified name. 只需遍历列表,找到与指定名称匹配的联系人。

/**
 * Returns the List index of the Contact with the specified name. If no such
 * Contact is found, -1 will be returned.
 */
public int findItem(String name) {
    for (int i = 0; i < contactList.size(); i++) {
        Contact contact = contactList.get(i);
        if (null == contact) continue;
        if (java.lang.Objects.equals(name, contact.getName())) return i;
    }
    return -1;
}

Just to add guys, I've been able to do it this way: 只是为了添加家伙,我已经能够这样做了:

public void searchItem(String name) {
    for(int i = 0; i < contactList.size(); i++) {
        if(name.equals(contactList.get(i).getName())) {
            System.out.println("Found " + name);
            break;
        }
        else {
            System.out.println("Could not find name!");
        }
    }
}

However, is this not fairly inefficient if I were to have a larger list? 但是,如果我有一个更大的名单,这不是很低效吗? Is there a more efficient way of doing this? 有更有效的方法吗?

If you are interested. 如果你感兴趣。 A better way is to override equals() and hashcode() in your object. 更好的方法是覆盖对象中的equals()和hashcode()。 And use the indexOf the proper way. 并以正确的方式使用indexOf。

Your equals could determine the equality based on the name, therefore removing all that extra and unnecessary code. 您的equals可以根据名称确定相等性,因此删除所有额外的和不必要的代码。

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

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