从零学Java 集合概述

Java 集合概述

文章目录

  • [Java 集合概述](#Java 集合概述)
    • [1 什么是集合?](#1 什么是集合?)
    • [2 Collection体系集合](#2 Collection体系集合)
      • [2.1 Collection父接口](#2.1 Collection父接口)
        • [2.1.1 常用方法](#2.1.1 常用方法)
        • [2.1.2 Iterator 接口](#2.1.2 Iterator 接口)

1 什么是集合?

概念:对象的容器,定义了对多个对象进行操作的常用方法;可实现数组的功能。
和数组区别

  • 数组长度固定,集合长度不固定。
  • 数组可以存储基本类型和引用类型,集合只能存储引用类型。

2 Collection体系集合

Collection体系结构的根接口,代表一组对象,称为"集合"。

  • List接口的特点:有序有下标、元素可重复
  • Set接口的特点:无序无下标、元素不能重复

2.1 Collection父接口

特点:代表一组任意类型的对象。

2.1.1 常用方法

eg:

java 复制代码
/**
 * @author 胡昊龙
 * @version 1.0
 * @description: TODO
 * @date 2024/1/10 11:09
 */
public class Test {
    public static void main(String[] args) {
        //Collection接口的使用
        //创建集合
        Collection collection = new ArrayList();
        Collection collection1 = new ArrayList();
        //1 添加单个元素
        collection.add("北京");
        collection.add("上海");
        collection.add("杭州");
        collection.add("哈尔滨");
        collection.add("北京");
        collection1.add("廊坊");
        collection1.add("保定");
        collection1.add("保定");
        //1.1 添加整个集合
        collection.addAll(collection1);
        System.out.println("元素个数: "+collection1.size());
        System.out.println("打印: "+collection1);
        System.out.println("元素个数: "+collection.size());
        System.out.println("打印: "+collection);
        //2 删除
        //2.1 删除一个元素
        collection.remove("北京");
        System.out.println("删除以后: "+collection);
        //2.2 清空
        collection1.clear();
        System.out.println("清空: "+collection1);
        //3 遍历
        //3.1 增强for
        System.out.println("增强for-----------");
        for (Object o : collection) {
            System.out.println(o);
        }
        //3.2 迭代器:集合中专门用来遍历集合的
        System.out.println("迭代器------------");
        Iterator it = collection.iterator();
        //3.2.1 it.hasNext(); 判断是否还有元素
        //3.2.2 it.next(); 获取下一个元素
        while (it.hasNext()){
            Object next = it.next();
            System.out.println(next);
        }
        //3.2.3 it.remove(); 删除元素
        //4 判断
        //4.1 判断元素是否存在
        System.out.println(collection.contains("哈尔滨"));
        //4.2 判断集合是否为空
        System.out.println(collection1.isEmpty());
    }
}
2.1.2 Iterator 接口

Iterator:迭代器用来遍历集合的统一接口。

  • hasNext();判断是否还有元素
  • next();获取元素
  • remove();删除元素。
java 复制代码
//3.2 迭代器:集合中专门用来遍历集合的
System.out.println("迭代器------------");
Iterator it = collection.iterator();
//3.2.1 it.hasNext(); 判断是否还有元素
//3.2.2 it.next(); 获取下一个元素
while (it.hasNext()){
    Object next = it.next();
    System.out.println(next);
}
//3.2.3 it.remove(); 删除当前元素
//it.remove();

注意

  • 不能执行多次next()。
  • 在使用迭代器过程中,不能使用集合的删除方法,只能使用迭代器的删除方法,
    否则出现并发修改异常(ConcurrentModificationException)
相关推荐
2301_7875528721 分钟前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
懵逼的小黑子25 分钟前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
Y3174291 小时前
Python Day23 学习
python·学习
Ai尚研修-贾莲2 小时前
Python语言在地球科学交叉领域中的应用——从数据可视化到常见数据分析方法的使用【实例操作】
python·信息可视化·数据分析·地球科学
格林威2 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
小林学习编程2 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端
撸码到无法自拔2 小时前
docker常见命令
java·spring cloud·docker·容器·eureka
橙子199110162 小时前
在 Kotlin 中什么是委托属性,简要说说其使用场景和原理
android·开发语言·kotlin
androidwork2 小时前
Kotlin Android LeakCanary内存泄漏检测实战
android·开发语言·kotlin
heart000_12 小时前
IDEA 插件推荐:提升编程效率
java·ide·intellij-idea