32Collection

54 阅读4分钟
### 集合与数组
 (1)数组 集合都是容器,都可以存储多个数据
        (2)数组集合都可以存储引用类型
         (3)数组长度不可变 集合长度可变
         (4)集合如果要存储基本类型,需要存储对应的包装类型
Collection  遍历toArray() clear() 
public class Demo01Collection {
    public static void main(String[] args) {
        //多态的方式创建Collection集合对象
        Collection<String> coll = new ArrayList<>();
    //add方法添加元素
        coll.add("Hello");    
     Collection<String> subColl = new ArrayList<>();
        subColl.add("Java");
        subColl.add("Python");
  //addAll方法: 把subColl集合中的所有元素添加到coll中
   coll.addAll(subColl);
    coll.remove("Python");
           //判断是否包含Java
        System.out.println("集合中是否包含Java: "+coll.contains("Java"));//true
     System.out.println("集合是否为空? "+coll.isEmpty());//false
        System.out.println("集合元素数量: "+coll.size());//3
}        
public class Demo01Collection {
    public static void main(String[] args) {  
        //遍历Collection集合对象coll
        for (int i = 0; i < coll.size(); i++) {
            //报错了: 把ArrayList集合对象已经当成了Collection接口使用
            //然而Collection接口中,并没有定义含有索引的方法
            //coll.get(i);
        }
        //toArray方法把Collection集合对象变成数组
        Object[] array = coll.toArray();
        //遍历数组
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("-----------"); 
        //清空集合
        coll.clear(); 
        System.out.println("集合是否为空? "+coll.isEmpty());//true
        System.out.println("集合元素数量: "+coll.size());//0
    }
}

遍历Collection- 获取迭代器接口Iterator的实现类对象it

遍历Collection- 获取迭代器接口Iterator的实现类对象it
 4.迭代器接口Iterator的实现类对象it调用hasNext方法,判断是否具有下一个元素
 如果有: 迭代器接口Iterator的实现类对象it调用next方法,获取下一个元素并输出
   public class Demo04Iterator {
    public static void main(String[] args) {
        //1.多态的方式创建Collection集合对象coll
        Collection<String> coll = new ArrayList<>();

        //2.Collection集合对象coll调用add方法添加数据
        coll.add("Hello");
        coll.add("World");
        coll.add("Java");
 //2.创建Collection集合对象coll,存储数据的类型是Student
        Collection<Student> coll2 = new ArrayList<>();

        //3.向Collection集合对象coll中添加多个Student对象
        coll2.add(new Student("张三", 18));
        //3.Collection集合对象coll调用iterator方法,获取迭代器接口Iterator的实现类对象it
        Iterator<String> it = coll.iterator();

        //4.迭代器接口Iterator的实现类对象it调用hasNext方法,判断是否具有下一个元素
        while (it.hasNext()) {
            //5.如果有: 迭代器接口Iterator的实现类对象it调用next方法,获取下一个元素并输出
            String str = it.next();
            System.out.println("元素: "+str);
        }
   }
}
Collection是接口,但是我们常用的是其子接口ListSet remove()  
     ArrayList查询快增删慢 linkedlist 查询慢增删快
 子接口List必然集合父接口Collection List毕竟是接口,要使用它的常用实现类ArrayList集合
    ArrayList集合既然是List接口的实现类,必然实现接口,覆盖重写抽象方法
 List<E>子接口:  特点:   1.有序: 保证存入和取出元素的顺序是一致的 2.有索引      3.可重复 
    Set<E>子接口:   特点:  1.无索引    2.不可重复   
hashset 无序 linkedhashset 有序
迭代器的使用步骤:
    1.创建Collection集合对象
    2.Collection集合对象中添加元素
    3.Collection集合对象调用iterator方法,获取迭代器对象
    4.获取迭代器对象调用hasNext方法判断是否具有下一个元素
    5.如果有下一个元素: 迭代器对象调用next方法,获取下一个元素并输出
    6.如果没有元素: 结束迭代过程
    

并发修改异常

/*
    并发修改异常
        原因: 使用迭代器遍历集合元素时,又通过集合对象本身调用集合的方法,修改了集合的长度
        解决方案:
            使用迭代器遍历集合元素时,不能通过集合对象本身调用集合的方法,修改集合的长度
            (1)使用Collection接口迭代器的方法修改长度: remove
            (2)使用List接口特有迭代器ListIterator中的方法修改长度: remove,add 
            2.为什么使用迭代器遍历Collection集合时,使用集合对象的remove方法删除倒数第二个元素,不报并发修改异常?因为不会 调用next

 */
public class Demo04IteratorException {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("AAA");
        list.add("BBB");
        list.add("CCC");
        list.add("DDD");
        //删除元素BBB
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if ("BBB".equals(s)) {
                //list.remove(s);//通过集合修改长度,有问题
                it.remove();//通过迭代器修改长度,没有问题
            }
            /*if ("CCC".equals(s)) {
                list.remove(s);//为什么: 通过集合对象的remove方法删除CCC(集合的倒数第二个元素),就不报并发修改异常的呢?因为不会 调用next
            }*/
        }
        System.out.println(list);
    }
}