Java中集合的方法和迭代器

集合

    数组的弊端:
        1.只能添加相同类型的元素(基本数据类型 和 引用数据类型 都能保存)

        2.长度一旦确定 就不能改变 要添加超出数组长度个数的元素 操作比较复杂

        集合由来 因为数组操作数据的弊端 用来代替数组

        集合特点:
            1.能添加不同类型的元素
            注意: 集合中只能添加 引用数据类型(只能添加 对象类型)
            2.长度可变

集合collection接口中的方法

集合中的基本方法
    没写泛型的时候
    将@SuppressWarnings({ "rawtypes", "unchecked" })
    放到类 上
    整个类都不会报黄
    集合可以保存不同数据类型的数据
    保存基本数据类型 是以自动装箱的形式 进行储存


        // 创建一个集合 多态的声明方式

        Collection collection = new ArrayList();
        //添加方法
        //什么时候会添加失败?
        //ArrayList中的add 不可能返回失败
        //不能返回失败 为什么还要设计返回值?  --- 思想
        //适用所有的子类 子接口
        boolean b1 =  collection.add("a");
        boolean b2 =   collection.add("b");
        boolean b3 =   collection.add("c");
        // 当你向集合当中添加基本数据类型的时候
        // 系统会帮你进行 自动装箱 把基本数据类型 变成 它的包装类
        boolean b4 =  collection.add(10);
        boolean b5 =  collection.add(true);
        boolean b6 =  collection.add('s');
        //打印集合
        System.out.println(collection.toString());
//      System.out.println(b1);
//      System.out.println(b2);
//      System.out.println(b3);
//      System.out.println(b4);
//      System.out.println(b5);
//      System.out.println(b6);

        //获取集合的长度
        System.out.println(collection.size());

        // 判断是否包含某个元素
        boolean b7 = collection.contains("b");
        System.out.println(b7);

        //从集合中删除一个元素
        boolean b8 = collection.remove(true);
        System.out.println(b8);
        // 操作的是 集合本身
        System.out.println(collection);

        //判断集合 是否为空
        boolean b9 = collection.isEmpty();
        System.out.println(b9);

        //清空数组
        collection.clear();
        System.out.println(collection);
添加
    public static void fun2() {
        Collection collection = new ArrayList();
        //添加 a b c d
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");

        System.out.println(collection);
        //遍历集合中的每一个元素


        Object[] array = collection.toArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println(Arrays.toString(array));
    }
强制转换
    前提: 需要创建一个学生类 有名字 和 年龄
    下面的Student就是创建出来的类 
    注意学生类的成员变量私有化 构造函数 set/get方法都要写

    public static void fun3() {
        /*
         * 创建一个集合
         * 保存3个学生
         * 遍历学生信息 只打印名字
         */
        Collection collection = new ArrayList();

        collection.add(new Student("sxm", 15));
        collection.add(new Student("ygs", 16));
        collection.add(new Student("xsd", 17));

        // 集合转数组(相当于 有一个 向上转型)

        Object[] array = collection.toArray();
        //遍历数组
        for (int i = 0; i < array.length; i++) {
            //array[i]直接从数组中取出来 是Object类型
            //要想使用 Student 类中的方法 需要 强转 向下转型
            //必须 是 这个类型 你才能强转
            Student student = (Student)array[i];
            System.out.println(student.getName());
        }
    }
addAll方法
    Collection collection = new ArrayList();
        collection.add("ygs");
        collection.add("sxm");
        collection.add("dp");

        //addAll是把前一个集合的东西 都放进这个数组里
        Collection collection2 = new ArrayList();
        collection2.add("y");
        collection2.add("s");
        collection2.add("d");
        collection2.addAll(collection);
        //给集合collection2 添加一个元素 这个元素是 collection
        //collection2.add(collection);
        //打印结果 [y, s, d, ygs, sxm, dp, [ygs, sxm, dp]]

        System.out.println(collection);

        System.out.println(collection2);
containsAll方法
     Collection collection = new ArrayList();
        collection.add("ygs");
        collection.add("sxm");
        collection.add("dp");

        //containsAll 就是 前一个集合的元素和这个元素都一样就返回true
        Collection collection2 = new ArrayList();
        collection2.add("ygs");
        collection2.add("sxm");
        collection2.add("dp");
        boolean containsAll = collection2.containsAll(collection);
        System.out.println(containsAll);
removeAll方法
    Collection collection = new ArrayList();
        collection.add("ygs");
        collection.add("sxm");
        collection.add("dp");

        //removeAll
        //这个方法  将两个集合的重合元素删除
        //谁调用这个方法 就删除 谁的元素 另一个集合 不变
        //只删除交集 重复也可以删
        Collection collection2 = new ArrayList();
        collection2.add("ygs");
        collection2.add("sxm");
        collection2.add("dp");
        collection2.add("wl");
        collection2.removeAll(collection);
        System.out.println(collection2);
retainAll
    Collection collection = new ArrayList();
        collection.add("ygs");
        collection.add("sxm");
        collection.add("dp");

        //retainAll
        // 把两个集合的交集取出来 保存在collection2中(谁调方法保存在谁那)
        // 如果collection 和 collection2 求出交集 放到 collection2中
        // 如果collection2和原来对比 发生变化 返回 true
        // 不发生变化 返回false
        Collection collection2 = new ArrayList();
        collection2.add("ygs");
        collection2.add("sxm");
        collection2.add("dp");
        collection2.add("wl");      
        boolean retainAll = collection2.retainAll(collection);
        System.out.println(collection2);
        System.out.println(retainAll);
    }

迭代器(遍历)

    public static void fun1() {
        //测试 迭代器中的方法
        Collection collection = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");

        //获取集合中的 迭代器
        Iterator iterator = collection.iterator();
        //先判断集合中是否有元素
        boolean isHasNext = iterator.hasNext();
        System.out.println(isHasNext);
        //从集合中取出元素
        Object next = iterator.next();
        System.out.println(next);
    }
遍历集合
    public static void fun2() {
        //测试 迭代器中的方法
        Collection collection = new ArrayList();
        //迭代时 有个一指针 指向 集合 首位置
        //每调用一次 next方法 这个指针向下移一格
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");

        Iterator iterator = collection.iterator();
        //如果有元素 就获取 没元素 就停止循环
        while (iterator.hasNext()) {
            // 注意: 迭代时 循环中 只能使用一次next()方法
            System.out.println(iterator.next());
        }
    }
强制类型转换
    public static void fun3() {
        /*
         * 创建一个集合
         * 保存3学生
         * 使用迭代器遍历 打印学生姓名
         */

        Collection collection = new ArrayList();
        collection.add(new Student("sxm",18));
        collection.add(new Student("ygs",19));
        collection.add(new Student("dp",20));
        //获取迭代器
        Iterator iterator = collection.iterator();
        //循环获取
        while (iterator.hasNext()) {
            //获取元素 (相当于 向上转型)
            Object next = iterator.next();
            //强转成Student类型(向下转型)
            Student student = (Student)next;
            //打印姓名
            System.out.println(student.getName());
        }
    }
                                                                          Day.15
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值