Collection 集合的使用

1. Collection 概述

image-20220321144726967

1.创建 Collection 集合的对象,并添加元素
 public static void main(String[] args) {
        //创建Collection 集合的对象
        Collection<String> c = new ArrayList<String>();

        //添加元素 boolean add(E e)
        c.add("Hello");
        c.add("World");
        c.add("Java");

        System.out.println(c);
        //输出结果: [Hello, World, Java]
    }

2. Collection 集合的常用方法

方法名说明
boolean add(E e)添加元素
boolean remove(Object o)从集合中移除指定的元素
void clear()清空集合中的元素
boolean contains(Object o)判断集合中是否存在指定的元素
boolean isEmpty()判断集合是否为空
int size()集合的长度,集合中元素的个数
2.1 Collection 的使用
    public static void main(String[] args) {
        //创建Collection 集合的对象
        Collection<String> c = new ArrayList<String>();

        //boolean add(E e) 添加元素
        //System.out.println(c.add("Hello")); //输出结果:true
        c.add("Hello");
        c.add("World");
        c.add("World");
        c.add("Java"); //输出结果:true  [Hello, World, World, Java]

        //boolean remove(Object o) 从集合中移除指定的元素
        //c.remove("World"); //输出结果:[Hello, World, Java]

        //void clear()  清空集合中的元素
        //c.clear(); //输出结果:[]

        //boolean contains(Object o)  判断集合中是否存在指定的元素
        //System.out.println(c.contains("Java")); //输出结果: true

        //boolean isEmpty()  判断集合是否为空
        //System.out.println(c.isEmpty()); //输出结果: false

        //int size()   集合的长度,集合中元素的个数
        System.out.println(c.size()); //输出结果: 4


        System.out.println(c);
    }

3. Collection 集合的遍历

3.1 Iterator 迭代器

image-20220321153228365

3.2 迭代器方法

image-20220321154952488

3.3 迭代器使用
 public static void main(String[] args) {
        Collection<String> c = new ArrayList<String>();
        c.add("Hello");
        c.add("World");
        c.add("Java");

        /*boolean hasNext() 如果迭代具有更多元素,则返回 true 。
          E next()   返回迭代中的下一个元素。
        */

        Iterator<String> it = c.iterator();

        /*
        System.out.println(it.next());//输出结果:Hello
        System.out.println(it.next());//输出结果:World
        System.out.println(it.next());//输出结果:Java
        System.out.println(it.next());// NoSuchElementException  被各种访问器方法抛出,表示被请求的元素不存在。
        */

        //改进代码
        while (it.hasNext()){
            //System.out.println(it.next());
            String s = it.next();
            System.out.println(s);
        }
        //输出结果:Hello World Java

    }

4.学生案例

1.学生类
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
2.测试类
public class StudentDemo {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<Student>();

        Student s1 = new Student();
        s1.setName("张嘉圣杰");
        s1.setAge(22);
        Student s2 = new Student("张飘飘", 21);
        Student s3 = new Student("佳洁", 20);

        //把学生添加到集合中
        c.add(s1);
        c.add(s2);
        c.add(s3);

        //遍历集合使用迭代器 Iterator
        Iterator<Student> it = c.iterator();

        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "\t,\t" + s.getAge());
        }


    }
}
3.输出结果

image-20220321171948567

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2023-8-13胖胖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值