155_集合_Collection集合的常用方法详解_03_contains
来源:互联网 发布:node会取代php吗 编辑:程序博客网 时间:2024/05/18 18:01
import java.util.ArrayList;import java.util.Collection;public class ContainsTest {public static void main(String[] args) {//建立集合Collection C = new ArrayList();//添加元素C.add(1);//判断是否包含System.out.println(C.contains(1));//trueInteger m = new Integer(50);C.add(m);Integer n = new Integer(50);//System.out.println(C.contains(n));//true 说明底层已经重写了equals方法Manager m1 = new Manager(100, "jack");C.add(m1);Manager m2 = new Manager(100, "jack");//如果两个引用的编号和姓名一致,那说明包含,这样把底层的equals方法进行重写//而contains在底层的时间使用equals方法判断是否重复System.out.println(C.contains(m2));//**********************************************contains(Object o)在ArrayList中的实现方法// 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 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;// }}}class Manager{ int number; String name; public Manager(int number,String name) {this.number=number;this.name=name;} public boolean equals(Object o){ if(this==o) return true; if (o instanceof Manager){ Manager s =(Manager)o; if(s.number==this.number && s.name.equals(this.name)){ return true; } } return false; }}
0 0
- 155_集合_Collection集合的常用方法详解_03_contains
- 156_07_集合_Collection集合的常用方法详解_04_remove
- 153_Collection集合的常用方法详解_01
- 167_集合_Map常用方法详解
- Java集合框架_三_Collection接口
- 集合框架_Collection
- Java_集合_Collection
- java集合(上)_Collection集合
- 集合框架_Collection集合的高级功能测试
- 集合的常用方法
- [黑马程序员]集合_Collection示例
- JavaSE_集合_Collection接口简介
- 0007_Collection、Map集合类
- Java 集合框架_Collection接口
- 【JavaSE笔记】集合(一)_Collection
- 为什么需要集合?集合的常用方法
- 集合框架_Collection集合的功能概述、Collection集合的基本功能测试
- 集合常用的方法--073
- 最大子段和
- webview加载html文件,如何调用原生态的代码的
- eclipse新浪云sae插件安装
- Shell 间接变量问题
- 实施CMMI3有感:CMMI绝对是治疗软件公司特大毒瘤的特效药!
- 155_集合_Collection集合的常用方法详解_03_contains
- Codeforces 793D Presents in Bankopolis DP
- 自定义AlertDialog在android-7.0上距离屏幕边界margin无法消除问题
- EmguCV 一些基本操作
- jq 获取option 选中的各种值
- "解决“就是当做信仰的存在
- 'OSError: .*.so: undefined symbol'
- 线性表—顺序存储结构-顺序表
- hadoop全分布式环境配置-访问slave节点路径不存在问题