indexOf()
/**
* 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 <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
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;
}
THINK:源码中对遍历null和非null做了区分处理,故怀疑null遍历时更快。demo证明在数据量小于千万级别下只有几毫秒的差异,在5千万时最大有80毫秒的差距。再多就抛OutOfMemoryError了。