I was wondering how the method indexOf of an ArrayList is implemented. In fact I have override the equals method like this:
public class CustomObject {
@Override
public boolean equals(Object o) {
if(o instanceof CityLoader)
return ((CityLoader)o).getName() == this.name;
else if (o instanceof String)
return this.name.equals((String)o);
return false;
}
}
I though this will avoid me to override also the indexOf method but it seems I am totally wrong. When I try
ArrayList<CustomObject> customObjects = new ArrayList<CustomObject>
... insert customobject into the arraylist ...
customObjects.indexOf(new String("name"))
indexOf return false but it should return true. (I checked the element I am looking for exists)
Am I totally wrong?
(o==null ? get(i)==null : o.equals(get(i)))
, or -1 if there is no such index." – Desilva