2.Collection集合

本文介绍了Java集合框架的基础知识,包括Collection接口及其常用的子接口如Set和List。讲解了如何创建并使用ArrayList集合,展示了添加、删除、查找、判断集合状态及获取元素数量的方法。此外,还详细阐述了迭代器Iterator的使用,以及如何遍历集合。最后,通过实例展示了如何存储和遍历学生对象的集合,巩固了集合的使用步骤。
摘要由CSDN通过智能技术生成

1.集合知识回顾

  • 集合类的特点:提供了一种存储空间可变的存储模型,存储的数据容量可以随时发生改变。

  • 集合类体系结构

在这里插入图片描述

2.Collection集合概述和使用

  • Collection集合概述

    • 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素。
    • JDK不提供此接口的任何直接实现,它提供更具体的子接口(如SetList)实现
  • 创建Collection集合的对象

    • 多态的方式
    • 具体的实现类ArrayList
  • 代码示例

package study;

import java.util.ArrayList;
import java.util.Collection;

/*创建`Collection`集合的对象

- 多态的方式
- 具体的实现类`ArrayList`*/
public class CollectionDemo {
    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);
    }
}

3.Collection集合常用方法

方法名说明
boolean add(E e)添加元素
boolean remove(Object o)从集合中删除指定的元素
void clear()清空集合中的元素
boolean contains(Object o)判断集合中是否存在指定的元素
boolean isEmpty()判断集合是否为空
int size()集合的长度,也就是集合中元素的个数
  • 示例代码
package study;

import java.util.ArrayList;
import java.util.Collection;

//        | `boolean add(E e)`           | 添加元素
//        | `boolean remove(Object o)`   | 从集合中删除指定的元素
//        | `void clear()`               | 清空集合中的元素
//        | `boolean contains(Object o)` | 判断集合中是否存在指定的元素
//        | `boolean isEmpty()`          | 判断集合是否为空
//        | `int size()`                 | 集合的长度,也就是集合中元素的个数

//alt+7:能够打开一个窗口,看到一个类的所有信息
public class CollectionDemo1 {
    public static void main(String[] args) {
//        创建集合对象
        Collection<String> c = new ArrayList<String>();

//        | `boolean add(E e)`           | 添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        c.add("java1");
        c.add("java");
        System.out.println(c);
        System.out.println("=======================");

//        | `boolean remove(Object o)`   | 从集合中删除指定的元素
        c.remove("java");//只删除第一个符合条件的元素
        System.out.println(c);
        System.out.println("=======================");

//        | `boolean contains(Object o)` | 判断集合中是否存在指定的元素
        boolean i = c.contains("hello");
        System.out.println(i);
        System.out.println("=======================");

//        | `boolean isEmpty()`          | 判断集合是否为空
        System.out.println("是否为空:" + c.isEmpty());
        System.out.println("=======================");

//        | `int size()`                 | 集合的长度,也就是集合中元素的个数
        System.out.println(c.size());
        System.out.println("=======================");

//        | `void clear()`               | 清空集合中的元素
        c.clear();
        System.out.println(c);
        System.out.println("是否为空:" + c.isEmpty());
   System.out.println("=======================");
    }
}

4.Collection集合的遍历

Iterator:迭代器,集合的专用遍历方式

  • Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到。
  • 迭代器是通过集合的iterator()方法得到的,所以它是依赖于集合而存在的。

Iterator中的常用方法

  • E next():返回迭代中的下一个元素

  • boolean hasNext():如果迭代具有更多元素,则返回true

  • 代码示例

package study;
//         `Iterator`:迭代器,集合的专用遍历方式
//        - `Iterator<E> iterator()`:返回此集合中元素的迭代器,通过集合的`iterator()`方法得到。
//        - 迭代器是通过集合的`iterator()`方法得到的,所以它是依赖于集合而存在的。

//        `Iterator`中的常用方法
//        - `E next()`:返回迭代中的下一个元素
//        - `boolean hasNext()`:如果迭代具有更多元素,则返回`true`。


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo2 {
    public static void main(String[] args) {
        //    创建一个集合对象
        Collection<String> c = new ArrayList<String>();

//    添加元素
        c.add("hello");
        c.add("world");
        c.add("java");

//        - `Iterator<E> iterator()`:返回此集合中元素的迭代器,通过集合的`iterator()`方法得到。
        Iterator<String> it = c.iterator();

//        `E next()`:返回迭代中的下一个元素
       /* System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());//NoSuchElementException:请求被访问元素不存在*/

//        - `boolean hasNext()`:如果迭代具有更多元素,则返回`true`。
       /* if (it.hasNext()){
            System.out.println(it.next());
        }
        if (it.hasNext()){
            System.out.println(it.next());
        }
        if (it.hasNext()){
            System.out.println(it.next());
        }
        if (it.hasNext()){
            System.out.println(it.next());
        }*/
//        改进
        while (it.hasNext()){
//            System.out.println(it.next());
            String s = it.next();
            System.out.println(s);
        }
    }
}

5.集合的使用步骤

在这里插入图片描述

案例

  • Collection集合存储学生对象并遍历

需求:创建一个存储学生对象的集合,存储三个学生对象,使用程序在控制台遍历该集合。

  • student类
package study01;

public class student {
    private String name;
    private int age;
    private  String sex;
    //构造方法
    public student(){}
    public student(String name,String sex,int age){
        this.age = age;
        this.name = name;
        this.sex  = sex;
    }
    //set、get方法
    public void setName(String name){
        this.name = name;
    }
    public void setSex(String sex){
        this.sex = sex;
    }
    public void setAge(int age){
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public String getSex(){
        return sex;
    }
    public int getAge(){
        return age;
    }
}
  • 测试类

  •   package study01;
      
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.Iterator;
      
      public class studentDemo {
          public static void main(String[] args) {
              //创建学生对象
              student s1 = new student();
              s1.setName("张三");
              s1.setAge(15);
              s1.setSex("男");
      
              student s2 = new student("李四","男",16);
              student s3 = new student("王二","女",17);
      
              //创建集合对象
              Collection<student> c = new ArrayList<student>();
              c.add(s1);
              c.add(s2);
              c.add(s3);
      
              //iterator
              Iterator<student> it = c.iterator();
              //遍历学生对象
              while (it.hasNext()){
                  student s4 = it.next();
                  studentOut(s4);
              }
      
          }
          private static void studentOut(student s){
              System.out.println("姓名:"+s.getName()+";  性别:"+s.getSex()+"; 年龄:"+s.getAge());
          }
      }
    

s4 = it.next();
studentOut(s4);
}

    }
    private static void studentOut(student s){
        System.out.println("姓名:"+s.getName()+";  性别:"+s.getSex()+"; 年龄:"+s.getAge());
    }
}
```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

幺洞两肆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值