简体   繁体   English

在Java中使用indexOf()查找对象arrayList的索引值

[英]Finding index value of an Object arrayList using indexOf() in Java

This method scans the current staff Employee array to see whether there is a match between the any member of staff and the Employee passed in. 此方法扫描当前的Staff Employee数组,以查看任何staff成员与传入的Employee之间是否匹配。

Return -1 if there is no match, and the index number of the Employee if there is a match. 如果不匹配,则返回-1;如果不匹配,则返回Employee的索引号。

Now my question is that, how can I use - 现在我的问题是,我该如何使用-

indexOf(); 指数();

method properly to get the index value of the object and in turn the employee whose empId matches with the employee passed in 正确地获取对象的索引值的方法,然后获取其empId与传入的员工匹配的员工

public int findEmployee(Employee emp) {
    int index = -1;
    for (Employee s : staff) {
        if (emp.getEmpId() == s.getEmpId()) {
            index = indexOf(); //how to use this
        }
    }

    return index;
}

I'm open to any other ways of comparing as I know indexOf() can search and find the empId for me. 我愿意采用其他任何比较方法,因为我知道indexOf() 可以为我搜索和找到empId So if I have to do away with the if statement all together I don't mind . 所以如果我必须一起放弃if语句 ,我不在乎 I think it will make the code more effective. 我认为这将使代码更有效。

Going from comments because an explanation of indexOf() would likely be too long for comments. 从注释中indexOf()因为对indexOf()的解释对于注释而言可能太长了。 Other answerers provide good alternatives, but I'll answer the way that is requested. 其他答题者提供了不错的选择,但我将按照要求的方式回答。

I'm assuming you're working with a List of some kind (eg staff is an ArrayList<Employee> ), as the Arrays utility class doesn't appear to have an indexOf() method. 我假设您正在使用某种List (例如, staffArrayList<Employee> ),因为Arrays实用程序类似乎没有indexOf()方法。

Unfortunately, I have no C++ experience, so I'm not sure what concepts in Java map over to C++ well. 不幸的是,我没有C ++经验,所以我不确定Java中的哪些概念可以很好地映射到C ++。 If you have any questions, feel free to ask. 如果你有任何问题随时问。

The javadoc for ArrayList#indexOf(Object o) states: ArrayList#indexOf(Object o)的Javadoc状态:

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. 返回指定元素在此列表中首次出现的索引;如果此列表不包含该元素,则返回-1。 More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) , or -1 if there is no such index. 更正式地,返回最低索引i,使其(o==null ? get(i)==null : o.equals(get(i))) ;如果没有这样的索引,则返回-1。

The interesting portion is this: 有趣的部分是:

returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) 返回最低索引i,以使(o==null ? get(i)==null : o.equals(get(i)))

So essentially, what indexOf() will do is loop over the list, until it finds an element such that: 因此从本质indexOf()indexOf()要做的是循环遍历列表,直到找到一个满足以下条件的元素:

  • if o == null , the element is also null 如果o == null ,则元素也为null
  • if o != null , o.equals(element) returns true 如果o != null ,则o.equals(element)返回true

Or if no such element exists, then -1 will be returned. 或者,如果不存在这样的元素,则将返回-1 I assume the employee you're passing in is non-null, I'll focus on the second option there. 我假设您要转入的员工为空,我将重点介绍第二个选项。

o.equals(element) is pretty self-explanatory. o.equals(element)很明显。 However, there is something to be careful for: If the Employee class does not override equals() , you'll likely not get the behavior you want. 但是,有一些注意事项:如果Employee类未覆盖equals() ,则可能无法获得所需的行为。 This is because the default implementation of equals() checks for reference equality (where the two references you're comparing point to the same underlying object, similar to what I'm guessing in C++ is two pointers pointing to the same location/object), not object equality (the "normal" equals, where two objects are equal if they "represent the same thing", even if they are distinct objects). 这是因为equals()的默认实现会检查引用是否相等 (您要比较的两个引用都指向同一个基础对象,类似于我在C ++中猜测的是指向同一位置/对象的两个指针) ,而不是对象相等 (“正常”相等,如果两个对象“代表同一件事”,则两个对象相等,即使它们是不同的对象也是如此)。

So it looks like in your case, if you want to match Employees by ID number, you'll need to write an equals() method that takes in an Object, checks to see if it's an Employee , and if it is, if the passed object's employee ID matches that of the employee you're calling equals() on. 因此,在您的情况下,如果要按ID号匹配Employees,则需要编写一个equals()方法,该方法接受一个Object,检查它是否为Employee ,是否为Employee 。传递的对象的员工ID与您要调用equals()的员工的ID匹配。 Of course, if such an equals() method already exists, then you don't have to do anything. 当然,如果已经存在这样的equals()方法,则无需执行任何操作。 If equals() is already overridden and has some different behavior, then you might be out of luck... 如果equals()已经被覆盖并且具有某些不同的行为,那么您可能不走运...

Also, be careful that the method signature you use is equals(Object o) , and not equals(Employee e) . 另外,请注意,您使用的方法签名是equals(Object o) ,而不是 equals(Employee e) Those are two different method signatures, and only the first will override Object#equals() . 那是两个不同的方法签名,只有第一个会覆盖Object#equals()

Once you have a proper equals() method written, indexOf() should do the rest of the work for you. 一旦编写了正确的equals()方法, indexOf()应该为您完成其余的工作。

If each Employee has its own unique id, then you need to implement equals() method inside Employee object and return true if ids are equal, eg: 如果每个Employee都有自己的唯一ID,则需要在Employee对象中实现equals()方法,如果ID相等,则返回true,例如:

@Override    
public boolean equals(Object object) {
    return (object instanceof Employee) && (id != null) 
         ? id.equals(((Employee) object).id) //change ".equals" to "=" if you use int instead of Integer, which I believe you apparently do.
         : (object == this);
}

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

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