Collection 是所有单列集合的父接口,因此在 Collection 中定义了单列集合(List 和 Set)通用的一些方法,这些方法可用于操作所有的单列集合

方法如下:
public boolean add(E e) :把给定的对象添加到当前集合中 。
public void clear() :清空集合中所有的元素。
public boolean remove(E e) : 把给定的对象在当前集合中删除。
public boolean contains(E e) : 判断当前集合中是否包含给定的对象。
public boolean isEmpty() : 判断当前集合是否为空。
public int size() : 返回集合中元素的个数。
public Object[] toArray() : 把集合中的元素,存储到数组中。
package day_02_Demo;

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

/*
public boolean add(E e) :把给定的对象添加到当前集合中 。
public void clear() :清空集合中所有的元素。
public boolean remove(E e) : 把给定的对象在当前集合中删除。
public boolean contains(E e) : 判断当前集合中是否包含给定的对象。
public boolean isEmpty() : 判断当前集合是否为空。
public int size() : 返回集合中元素的个数。
public Object[] toArray() : 把集合中的元素,存储到数组中。
* */
public class Demo01Collection {
    public static void main(String[] args) {
        //创建集合对象
        Collection<String> coll = new ArrayList<String>();
        coll.add("tom");
        coll.add("张三");
        coll.add("lili");
        //打印集合
        System.out.println(coll);
        //contains
        System.out.println(coll.contains("tom"));
        //remove
        System.out.println(coll.remove("tom"));
        System.out.println(coll);

        //size
        System.out.println(coll.size());//2
        //toArray
        Object[] arry = coll.toArray();
        for (int i = 0; i <arry.length ; i++) {
            System.out.print(arry[i]+",");
        }
        System.out.println(" ");
        System.out.println("============================");
        //clear
        coll.clear();
        System.out.println(coll);
        //isEmptiy
        System.out.println(coll.isEmpty());

        //
    }
}
posted @ 2022-03-26 15:57  不只是智能hello  阅读(383)  评论(0)    收藏  举报