Bug in ArrayList
aagarw33 opened this issue · 5 comments
In contains method of ArrayList if condition to check if value is equal should return false, when the condition is not satisfied.
I don't think that's correct. You have to check each element in the array before you can return false. Maybe I don't follow, what looks incorrect to you?
public boolean contains(T value) {
for (int i = 0; i < size; i++) {
T obj = array[i];
if (obj.equals(value)) return true;
}
return false;
}
Sorry for closing and reopening. I was doing a test this morning. I got NPE when value is not found.
Can you give me an example, I can't think of any situation which'll cause a NPE in the contains method. if obj is NULL that may happen but that shouldnt happen.
What you said is correct, I am getting NPE when the obj in NULL - I am searching for value that is not in the list, which I think is a valid use case. For this instead if returning FALSE I get the NPE.
The only way I can think of to get obj==NULL is if you have inserted NULL which I may not be checking. I'll look at the code later and see if that's possible.