Java笔记007-Collection

Collection是所有单列集合最顶层的接口,里面定义了所有单列集合的共性方法:
(单列集合:Vector、ArrayList、LinkedList、TreeSet、HashSet、LinkedHashSet)

1.public boolean add(E e); 向集合中添加元素:

        Collection<String> coll = new ArrayList();
        coll.add("张三");
        coll.add("李四");
        coll.add("王五");
        coll.add("赵六");
        coll.add("田七");
        System.out.println(coll); // [张三, 李四, 王五, 赵六, 田七] 

2.public boolean remove(E e); 删除集合中的指定元素:

        boolean b1 = coll.remove("田七");
        System.out.println(b1); // true
        System.out.println(coll); // [张三, 李四, 王五, 赵六]

3.public int size(); 得到集合的大小:

        int size = coll.size();
        System.out.println(size); // 4

4.public boolean contains(E e); 判断对象是否在集合中:

		boolean b2 = coll.contains("张三");
        System.out.println(b2); // true

5.public boolean isEmpty(); 判断集合是否为空:

        boolean b3 = coll.isEmpty();
        System.out.println(b3); // false

6.public Object[ ] toArray(); 将集合转换成数组:

        Object[] array = coll.toArray();
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " "); // 张三 李四 王五 赵六
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值