As far as I can see, there are a few problems with your code.
First of all, using break
is not very good practice. Especially using it where you've used it. In your code, after the if
statement processes, the for
loop ends. Regardless of the result of the if
statement, the for
loop will not iterate. So unless you only want to check if the desired employee is at index 0, this is far from desirable.
for (int i = 0; i < arr.size(); i++)
This is the second issue problem. It works well enough in the ideal case due to the break
statement you use, but if the desired employee is NOT in the ArrayList, you'll get an IndexOutOfBoundsException
because on the last iteration, you'll be trying to get the index of the size of the ArrayList, which doesn't exist. Instead, your for loop should be
for (int i = 0; i < arr.size() - 1; i++)
This way, in the event of the employee not being present, the loop will stop when it gets to the last index of the ArrayList, which is 1 less than the size.
Another, more immediate problem is this line:
if(str.equalsIgnoreCase(arr.get(obj2[i].getName())))
In this line, you're calling arr.get
with an Employee object as the argument, when you should be using an index of the ArrayList.
if(str.equalsIgnoreCase(arr.get(i).getName()))
With this modification, you call an index of the ArrayList, which will return an Employee object. You then call getName()
on that object, and it returns the string, which is compared for equality to str
, like you intend to do.
One last problem is the line where you remove the object.
arr.remove(obj2[i]);
Again, you seem to be trying to access an index of the Employee object, rather than the ArrayList. Changing this line to:
arr.remove(i);
would be much more effective. This way, you're removing the index of the array. You CAN use an object as an argument for the remove() method, but you would first need to assign the object at i
to obj2
, and it's altogether far more troublesome.
So, putting everything together, the new, fixed loop would be
for (int i = 0; i < arr.size() - 1; i++){
if(str.equalsIgnoreCase(arr.get(i).getName())){
arr.remove(i);
}
}
Assuming that there will only be one employee with a given name, this will not be a problem, but be warned that there are issues with removing objects inside ArrayLists while iterating through the lists. For example, if you remove the object at i
, then the objects after it in the ArrayList all shift downward by one index. Then if you iterate again, you're accessing the object at i+1
in the List. BUT, the ACTUAL object at i+1
is the object that was originally at i+2
. So if you remove an object in the ArrayList, you'll skip examining the object coming directly after it on the next iteration.
One simple solution to this is to iterate BACKWARDS through the ArrayList, so that when an object is removed from the list, all the objects that shift down one index have already been examined. There are other, more proper solutions to this, but iterating backwards is a simple way around it.