JAVA 集合一(Collection)


一、集合类体系

 二、Collection集合概述和基本使用

        概述:1.是单列集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素  2 JDK不提供此接口的任何直接实现,它提供更具体的子接口,如Set、List实现

       创建方式:1.多态方式  2.具体的实现类ArrayList 例如:Collection collection=new ArrayList();    

三、Collection常用方法

 public static void main(String[] args) throws IOException {

        //创建Collection集合对象
        Collection col=new ArrayList();
        //Boolean add(E e) 添加元素
        col.add("张三");
        col.add("李四");
        //boolean remove(E e) 从集合中移除指定的元素
        col.remove("李四");
        //void clear() 清空集合中的元素
        col.clear();
        //bool contains(E e) 判断集合中是否存在指定的元素
        col.contains("张三");
        //boolean isEmpty() 判断集合是否为空
        col.isEmpty();
        //int size() 集合的长度
        col.size();
        for (String str :
                col) {
            System.out.println(str);
        }
}

  

四、Collection集合的遍历

public static void main(String[] args) throws IOException {

        //创建Collection集合对象
        Collection col=new ArrayList();
        //Boolean add(E e) 添加元素
        col.add("张三");
        col.add("李四");
        //方式一
        Iterator it=col.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        //方式二
        Object[] objects=col.toArray();
        for (int i=0;i