Here is the source code:
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
}
return -1;
}
Why doesn't one this: have only one loop and the if-statement.
public int indexOf(Object o) {
for (int i = 0; i < size; i++){
if (o == null) {
if (elementData[i]==null)
return i;
else {
if (o.equals(elementData[i]))
return i;
}
}
return -1;
}
The first snippet has to have two loops, but some say the above code performance is good. Why?