Frage

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.

Return -1 if there is no match, and the index number of the Employee if there is a match.

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

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. So if I have to do away with the if statement all together I don't mind. I think it will make the code more effective.

War es hilfreich?

Lösung

Going from comments because an explanation of indexOf() would likely be too long for comments. 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 (e.g. staff is an ArrayList<Employee>), as the Arrays utility class doesn't appear to have an indexOf() method.

Unfortunately, I have no C++ experience, so I'm not sure what concepts in Java map over to C++ well. If you have any questions, feel free to ask.

The javadoc for ArrayList#indexOf(Object o) states:

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. 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.

The interesting portion is this:

returns the lowest index i such that (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:

  • if o == null, the element is also null
  • if o != null, o.equals(element) returns true

Or if no such element exists, then -1 will be returned. 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. 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. 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).

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. Of course, if such an equals() method already exists, then you don't have to do anything. If equals() is already overridden and has some different behavior, then you might be out of luck...

Also, be careful that the method signature you use is equals(Object o), and not equals(Employee e). Those are two different method signatures, and only the first will override Object#equals().

Once you have a proper equals() method written, indexOf() should do the rest of the work for you.

Andere Tipps

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:

@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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top