在开发中经常用到ArrayList,有时候需要判断ArrayList中,是否已经存在某一个自定义的实体对象,该如何判断呢?
ArrayList的Api文档中有这样一个方法:boolean contains(Object o),用来判断是否存在某一个Object。
我们先来看一下这个方法实现的具体源代码:
/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain th