0

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.

Saif Azmi
  • 63
  • 6
  • Have you read the `indexOf()` documentation? – awksp May 10 '14 at 20:39
  • Couldn't you use a hashmap with the employee id as the key? Are there multiple entry's with the same employee id? – GoldenJam May 10 '14 at 20:40
  • Take a look at this page: http://www.tutorialspoint.com/java/java_string_indexof.htm – Fraser Price May 10 '14 at 20:43
  • Are you aware that you can use an int variable for the index when looping over staff and return that? Other than that, agreeing with user3580294 – TeTeT May 10 '14 at 20:43
  • I'm kind of new to java. I'm experienced in C++ programming. @user3580294 - yes I have n I'm not able to make sense of it this time. – Saif Azmi May 10 '14 at 20:47
  • Don't you mean staff.getEmpId() too? – Fraser Price May 10 '14 at 20:48
  • @GoldenJam - so since im new to java I dnt know much about Hashmap n im trying to follow an activity guide so I would like to take things step by step. – Saif Azmi May 10 '14 at 20:50
  • @TeTeT - no I'm not aware of that if u cud plz explain further. - Thank you – Saif Azmi May 10 '14 at 20:51
  • Guyz If im being too naive. Then can u point a better way to tackle this problem from a beginners perspective. - Thank you – Saif Azmi May 10 '14 at 20:52
  • I would recommend looking into collections if you are new to java. Lists/maps etc will make things a lot easier. It makes code easier to read and avoids the complexity of arrays/indexes. – GoldenJam May 10 '14 at 20:56
  • **A good way is use `Map` instead of `ArrayList` to find the records by the EmployeeID as key.** Map is most efficient than ArrayList. – Braj May 10 '14 at 21:08
  • @GoldenJam - what are these collections exactly? n how do I access them? – Saif Azmi May 10 '14 at 21:08
  • @Braj - ok I will learn about maps n try to implement them. – Saif Azmi May 10 '14 at 21:09

2 Answers2

1

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.

awksp
  • 11,764
  • 4
  • 37
  • 44
  • Sorry if this seems a bit condescending, again, I'm not really familiar with C++ so I don't know what concepts they have in common and where they differ. – awksp May 10 '14 at 21:08
  • Thank u so much! :) u finally answered it for me. All i needed was this and explanation. I already have enough programming experience to come up with the alternatives provided by others. All I was looking for, was to be more java specific n utilize some java features to solve this problem. – Saif Azmi May 10 '14 at 21:18
  • If i have any further question can I ask later? – Saif Azmi May 10 '14 at 21:22
  • @user3624184 No problem! Glad I could help! And of course, feel free to ask questions whenever. – awksp May 10 '14 at 21:28
  • @user3624184 And remember, the online API is your best friend, especially for the built-in Java libraries. The source code should also be available if you have the JDK, so that's another resource. – awksp May 10 '14 at 21:29
0

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);
}
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24