ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java之Collection 体系集合

2021-11-17 15:05:50  阅读:116  来源: 互联网

标签:Java System collection println Collection Student 集合 out


Collection 体系集合

在这里插入图片描述

Collection 父接口

  • 特点:代表一组任意类型的对象,无序、无下标、不能重复

  • 方法:

在这里插入图片描述

  • public class Demo01 {
        /*Collection接口的使用
        1.建立元素
        2.删除元素
        3.遍历元素
        4.判断
         */
        public static void main(String[] args) {
    //        创建集合
            Collection collection = new ArrayList();
    //        1.建立元素
            collection.add("苹果");
            collection.add("榴莲");
            collection.add("西瓜");
            System.out.println("元素数量"+collection.size());
            System.out.println(collection);
    //        2.删除元
    //        collection.remove("西瓜");
    //        collection.clear();
    //        System.out.println("元素数量"+collection.size());
    //        3.遍历元素 重点!
    //        (1)增强for
            for(Object object : collection){
                System.out.println(object);
            }
    //        (2)使用迭代器(专门用来遍历集合的一种方式)
    //         hasNext(); 有没有下一个元素
    //         next(); 获取下一个元素
    //        remove(); 删除当前元素
            System.out.println("---------------------------------------------");
            Iterator it = collection.iterator();
            while (it.hasNext()){
                String object = (String)it.next();
                System.out.println(object);
    //            使用过程中 不能使用 collection 删除方法
    //            collection.remove(object);
    //            it.remove();
            }
            System.out.println("元素数量"+collection.size());
    //        4.判断
            System.out.println(collection.contains("西瓜"));
            System.out.println(collection.isEmpty());
    
        }
    }
    

Collection 使用 保存学生类对象

  • public class Demo02 {
        /*
        Collection的使用   保存学生类
         */
        public static void main(String[] args) {
    //        新建Collection对象
            Collection collection = new ArrayList();
            Student s1 = new Student("张三",20);
            Student s2 = new Student("张四",21);
            Student s3 = new Student("张五",22);
    //        添加数据
            collection.add(s1);
            collection.add(s2);
            collection.add(s3);
            System.out.println("元素个数:"+collection.size());
            System.out.println(collection.toString());
    //        删除
    //        collection.remove(s1);
    //        collection.clear();
    //        System.out.println("元素个数:"+collection.size());
    //        遍历
    //        (1)增强for
            for (Object object : collection) {
                Student s = (Student)object;
                System.out.println(s.toString());
    
            }
            //        (2)使用迭代器
    //         hasNext(); 有没有下一个元素
    //         next(); 获取下一个元素
    //        remove(); 删除当前元素
    //        迭代过程中不能使用Collection删除方法
            Iterator it = collection.iterator();
           while (it.hasNext()){
               Student s =(Student) it.next();
               System.out.println(s.toString());
           }
    //     判断
            System.out.println(collection.contains(s1));
            System.out.println(collection.isEmpty());
        }
    }
    

标签:Java,System,collection,println,Collection,Student,集合,out
来源: https://blog.csdn.net/xiaoxiaobaie/article/details/121378362

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有