zoukankan      html  css  js  c++  java
  • Collection接口-常用方法

    Collection接口-集合

    Collection接口-常用方法

    1、add()方法

    boolean add(E e)  //向集合中插入一个元素
    
    Collection c = new ArrayList();
    c.add("hello world");
    c.add(100);
    c.add(3.14);
    c.add(true);
    

    2、clear()方法

    void clear()  //清空集合中的元素,从这个集合中移除所有的元素
    
    Collection c = new ArrayList();
    c.clear();
    

    3、isEmpty()方法

    boolean isEmpty()  //返回 true如果集合不包含任何元素。判断集合是否为空
    
    Collection c = new ArrayList();
    c.add("hello world");
    c.add(100);
    c.add(3.14);
    c.add(true);
    boolean bool = c.isEmpty(); // false
    

    4、contains()方法

    boolean contains(Object o)  //返回 true如果集合包含指定元素,判断集合是否包含此元素
    
    Collection c = new ArrayList();
    c.add("hello world");
    c.add(100);
    c.add(3.14);
    c.add(true);
    boolean bool = c.contains(100); //true
    boolean bools = c.contains(200); //false
    

    5、size()方法

    int size()  //返回此集合中的元素的数目。返回集合中的元素个数
    
    Collection c = new ArrayList();
    c.add("hello world");
    c.add(100);
    c.add(3.14);
    c.add(true);
    int i = c.size(); // 4
    c.clear();
    int j = c.size(); // 0
    

    6、remove()方法

    boolean remove(Object o)  //从这个集合中移除指定元素的一个实例,如果它是存在的。删除集合中的一个元素
      
    Collection c = new ArrayList();
    c.add("hello world");
    c.add(100);
    c.add(3.14);
    c.add(true);
    boolean bool = c.remove(100); //true
    int i = c.size(); // 3
    boolean bools = c.remove(200); //false
    

    7、toArray()方法

    Object[] toArray()  //  返回包含此集合中所有元素的数组。 
      
    Collection c = new ArrayList();
    c.add("hello world");
    c.add(100);
    c.add(3.14);
    c.add(true);
    
    Object[] objects = c.toArray(); // 返回一个Object类型的数组,并把集合中的所有元素,存在数组中

     我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=18m85a2xkwc60

  • 相关阅读:
    JVM参数说明介绍
    使用Intellij IDEA的Bookmarks
    js中对小数取整
    idea 中pom.xml依赖版本号报错(报红,如下图所示)
    Springboot项目启动后访问不到Controller
    pringBoot Controller接收参数的几种常用方式
    Spring启动执行流程梳理
    SQL条件语句(IF, CASE WHEN, IF NULL)
    获取tomcat服务器上的部分日志
    Linux下 SpringBoot jar项目后台运行、查看、停用
  • 原文地址:https://www.cnblogs.com/mcxfate/p/13404754.html
Copyright © 2011-2022 走看看