Collection集合常用功能

import java.util.ArrayList;
import java.util.Collection;

public class Demo01Collection {
    public static void main(String[] args) {
        //创建集合对象,可以使用多态
        Collection<String> coll=new ArrayList<>();
        System.out.println(coll);//[]  重写了toString方法

        /*
        public boolean add(E e):把给定的对象添加到当前的集合中。
         */
        boolean b1=coll.add("张三");
        System.out.println("bi:"+b1);
        System.out.println(coll);//[张三]
        coll.add("李华");
        coll.add("李雷雷");
        System.out.println(coll);//[张三, 李华, 李雷雷]
        coll.remove("李华");//返回的是boolean值
         System.out.println(coll);
         /*
         public boolean contains(E e):判断当前集合中是否包含给定的对象
         包含返回ture 否则返回false
          */
        boolean b2 = coll.contains("张三");
        System.out.println(b2);

        boolean b3 = coll.isEmpty();
        System.out.println("b3:"+b3);//判断集合是否为空

        int size = coll.size();
        System.out.println("size:"+size);//返回集合中元素的个数
        //public Object[] toArray():把集合中的元素,存储到数组中
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //public void clear();清空集合中所有的元素。但是不删除集合,集合都还存在
        coll.clear();
        System.out.println(coll);
        System.out.println(coll.isEmpty());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值