Collecting

本文通过一个Java代码示例介绍了如何使用ArrayList集合进行基本操作,包括添加元素(add)、删除元素(remove)、检查元素是否存在(contains)以及获取集合大小(size)等。此外,还展示了清空集合(clear)的方法。
摘要由CSDN通过智能技术生成

Collecting

package Demo04;

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);
        /*
        public boolean add(E e); 添加对象到集合中
         */
        boolean b1= coll.add("刘二狗");
        System.out.println("b1: "+b1);
        System.out.println(coll);
        coll.add("老村长");
        coll.add("廖境泽");
        coll.add("江户川二狗");
        System.out.println(coll);
        /*
        public boolean remove(E e);
        删除对象
        存在 删除元素返回true
        否则 返回 false
         */
        boolean b2=coll.remove("lcz");
        boolean b3=coll.remove("老村长");
        System.out.println("b2: "+b2);
        System.out.println("b3: "+b3);
        System.out.println(coll);
        /*
        public boolean contains(E e);
        判断是否包含此元素
         */
        boolean b4=coll.contains("lcz");
        boolean b5=coll.contains("刘二狗");
        System.out.println("b4: "+b4);
        System.out.println("b5: "+b5);
        // public boolean isEmpty();
        boolean b6=coll.isEmpty();
        System.out.println("b6: "+b6);
        int size=coll.size();
        System.out.println("size:"+size);
        // public Object[] toArray();
        Object[] arr=coll.toArray();
        for (int i = 0; i < coll.size(); i++) {
            System.out.println(arr[i]);
        }
        coll.clear();
        System.out.println(coll);


    }
    //b1: true
    //[刘二狗]
    //[刘二狗, 老村长, 廖境泽, 江户川二狗]
    //b2: false
    //b3: true
    //[刘二狗, 廖境泽, 江户川二狗]
    //b4: false
    //b5: true
    //b6: false
    //size:3
    //刘二狗
    //廖境泽
    //江户川二狗
    //[]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值