Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在arrayList中将IndexOf与customObject一起使用_Java_Arraylist_Indexof - Fatal编程技术网

Java 在arrayList中将IndexOf与customObject一起使用

Java 在arrayList中将IndexOf与customObject一起使用,java,arraylist,indexof,Java,Arraylist,Indexof,我希望能够使用indexOf方法返回一个对象的位置,但只想传递联系人的名称来搜索它,有什么方法可以做到这一点吗 我目前有以下方法: private static ArrayList<Contacts> contactList = new ArrayList<Contacts>(); public class Contacts { private String name; private String number; public Contacts(String na

我希望能够使用indexOf方法返回一个对象的位置,但只想传递联系人的名称来搜索它,有什么方法可以做到这一点吗

我目前有以下方法:

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);
}
private static ArrayList contactList=new ArrayList();
公共类联系人{
私有字符串名称;
私有字符串编号;
公共联系人(字符串名称、字符串编号){
this.name=名称;
这个数字=数字;
}
公共字符串getName(){
返回名称;
}
公共字符串getNumber(){
返回号码;
}
公共void集合名(字符串名){
this.name=名称;
}
公共无效集合号(字符串号){
这个数字=数字;
}
公共int findItem(字符串名称){
返回contactList.indexOf(姓名);
}

这里有一个函数可以实现这一点,而无需遍历整个列表,我认为其复杂性小于O(n):

public int findItem(字符串名称)
{
int max=contactList.size();
//你可能得把这个减去一
//我不太清楚
int descCnt=最大值;

因为(int cnt=0;cnt您所要求的内容不在的合同中,所以不,您不应该试图让列表以这种方式工作

相反,你可以编写你自己的方法,相对容易地完成你想要的。只需在你的列表上迭代,找到与指定姓名匹配的联系人

/**
 * 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;
}
/**
*返回具有指定名称的联系人的列表索引。如果没有
*找到联系人时,将返回-1。
*/
公共int findItem(字符串名称){
对于(int i=0;i
为了增加男生,我可以这样做:

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!");
        }
    }
}
public void searchItem(字符串名称){
对于(int i=0;i

但是,如果我有一个更大的列表,这不是效率很低吗?有没有更有效的方法来做到这一点呢?

如果你按姓名大量查找
联系人,你可以将实例放入
映射中。
映射的具体类型取决于你的要求;
HashMap

您可以使用以下命令,而不是
联系人列表。添加(联系人)

contactMap.put(contacts.getName(), contacts);
然后使用以下命令在地图中查找项目:

contactMap.get(someName);
这将比每次扫描列表更快:对于
HashMap
,每次查找都是
O(1)
,而对于列表,每次查找都是
O(n)
。但是,它使用了更多的内存


顺便说一句,您的
Contacts
类看起来像是表示一个联系人,因此应该将其命名为单数:
contact

另外,您的
find
方法当前被声明为实例方法:

public int findItem(String name) {
这意味着您实际上需要一个
联系人的实例
来查找另一个
联系人的实例
。相反,将其声明为
静态

public static int findItem(String name) {
然后,您可以在没有实例的情况下调用它:

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

如果您感兴趣,更好的方法是重写对象中的equals()和hashcode(),并使用适当的索引


您的等号可以根据名称确定等号,从而删除所有多余和不必要的代码。

不,仍然是O(n)。这种“优化”可能会使事情变得更慢,因为从列表的任意一端获取项目可能会导致更多缓存回迁。复杂性为O(n)。您可能在for循环中进行了一半的迭代,但您仍在检查每个元素。这并不比逐个迭代整个过程好。我明白了。感谢您的澄清。我觉得这样做平均可以让您更快地找到答案@AndyTurner@SamOrozco如果您有
n
项,那么您将无法做得更好最坏的情况是检查所有
n
项。如果可以预处理列表,则可以做得更好:如果列表已排序,则可以在
O(log n)
中执行。如果可以将项目放在名到项的
HashMap
中,则可以在
O(1)
的“我不确定”中执行如果
descCnt==contactList.size()
,则
contactList.get(descCnt)
将抛出一个
IndexOutOfBoundsException
。请注意,您可以在for循环声明中声明和修改
descCnt
(int cnt=0,descCnt=max-1;cnt低效?您的应用程序中是否遇到了这样的性能问题,您需要担心如何优化您所编写的内容?请记住:过早优化是万恶之源。您在此处编写的内容是搜索列表的一种好方法。
联系人列表
是一种
ArrayList
,因此这将是相当有效的(除了打印
在每次迭代中都找不到name
,直到找到项目为止)。如果将其更改为
链接列表
(例如),效率会较低,因为列表检索在链接列表中是
O(n)
(vs
O(1)
在类似于
ArrayList
)的
随机访问列表中。@SamOrozco-So?这个问题没有提到性能问题。即使如此,这种方法对任意ArrayList的性能也是一样好。现在,如果数组按名称排序,我们可以进行更奇特的搜索,并将复杂性降低到O(ln(n)),但同样,这个问题没有提到复杂性要求和可排序性。很抱歉,我之前的评论让我感到困惑。
Contacts found = Contacts.find("name");