Collection集合和Collection的常用功能

image

boolean add(E e); 向集合里添加元素
boolean remove(E e); 删除集合中的某个元素
void clear(); 清空集合的所有元素
boolean contains(E e); 判断集合中是否包含某个元素
boolean isEmpty(); 判断集合是否为空
int size(); 获取集合的长度
Object[] toArray(); 将集合转成一个数组

package com.yang.june;

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

public class Test {
    public static void main(String[] args) {


        Collection<String> list = new ArrayList<>();

        list.add("abc");
        list.add("efg");
        list.add("hello");

        boolean empty = list.isEmpty();
        System.out.println(empty);//false

        int size = list.size();
        System.out.println(size);//3

        Object[] objects = list.toArray();
        for (Object object : objects) {
            System.out.println(object);//abcefghello
        }


        boolean abc = list.contains("abc");
        System.out.println(abc);//true

        boolean remove = list.remove("abc");
        System.out.println(remove);//true

        list.clear();

        System.out.println(list);//[]



    }
}

posted @ 2022-07-05 08:53  我滴妈老弟  阅读(29)  评论(0)    收藏  举报