[黑马程序员]集合_ArrayList的contains方法用的是equals
2014-03-18 21:31
274 查看
/** * 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;//调用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) {//分两种,一种是判断null,一种是非null对象 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]))//调用的equals,也就是如果不覆写equals方法,有可能达不到期望的效果 return i; } return -1; }
相关文章推荐
- [黑马程序员]集合_ArrayList练习时为何要重写equals方法
- ArrayList的contains方法,底层调用了equals方法
- 向ArrayList集合中存入对象,并以让集合用自己设定的方式去除重复元素!(覆写equals方法)
- [黑马程序员]集合_HashSet的equals方法
- 黑马程序员-集合(二)contains()方法的内部探索
- ArrayList的contains方法与Object的equals方法的关系
- 黑马程序员 集合里的对象所属类要按需求重写equals方法
- ArrayList的contains方法,底层调用了equals方法。
- 集合中的contains方法怎么去实现,以及怎么重写对象的equals方法?
- 黑马程序员——equals方法和hashCode方法的问题
- 黑马程序员---集合框架工具类Collections,数组工具类Arrays常见方法以及1.5版本新特性(高级for,可变参数和静态导入)
- 黑马程序员-java基础-去除ArrayList中重复的字符串方法2
- 黑马程序员----"=="和equals方法区别
- List集合去除重复对象及equals()、hashCode()方法的作用
- 集合框架-ArrayList集合的toString()方法源码解析
- 黑马程序员 模仿ArrayList去实现一个自定义的集合
- 黑马程序员-ArrayList集合深入学习
- 黑马程序员,collection集合方法
- 集合类 Contains 方法 深入详解 与接口的实例
- (转载)ArrayList的contains方法