当前位置: 首页 > ai >正文

Kotlin 中集合遍历有哪几种方式?

1 for-in 循环(最常用)

val list = listOf("A", "B", "C")
for (item in list) {print("$item ")
}// A B C 

2 forEach 高阶函数

val list = listOf("A", "B", "C")
list.forEach { item ->print("$item ")
}// A B C 

3 带索引遍历

3.1 使用 forEachIndexed
val list = listOf("A", "B", "C")
list.forEachIndexed { index, item ->println("$index - $item")
}// 0 - A
// 1 - B
// 2 - C
3.2 使用 withIndex() + for-in

使用 withIndex() 生成包含索引和值的迭代器。

val list = listOf("A", "B", "C")
for ((index, item) in list.withIndex()) {println("$index - $item")
}// 0 - A
// 1 - B
// 2 - C
4 传统索引遍历(适用于 List

通过集合的 indices 属性获取索引范围。

val list = listOf("A", "B", "C")
for (i in list.indices) { // println("${list[i]} at $i")
}// A at 0
// B at 1
// C at 2

5 迭代器(iterator

val list = listOf("A", "B", "C")
val iterator = list.iterator()
while (iterator.hasNext()) {print("${iterator.next()} ")
}// A B C 

6 List 专用:双向遍历(ListIterator

val list = listOf("A", "B", "C")
val listIterator = list.listIterator()
while (listIterator.hasNext()) {print("${listIterator.next()} ")
}println()while (listIterator.hasPrevious()) {print("${listIterator.previous()} ")
}// A B C 
// C B A 

7 while 循环(手动控制索引)

val list = listOf("A", "B", "C")
var i = 0
while (i < list.size) {print("${list[i]} ")i++
}
// A B C 

8 map 遍历转换(返回新集合)

val list = listOf("A", "B", "C")
val newList = list.map { item ->"Item: $item"
}
println(newList)// [Item: A, Item: B, Item: C]

9 区间遍历(for + until/downTo

适用于需要自定义步长或方向的场景:

val list = listOf("A", "B", "C")
for (i in 0 until list.size) {print("${list[i]} ")
}
// A B C val list = listOf("A", "B", "C")
for (i in list.size - 1 downTo 0) {print("${list[i]} ")
}
// C B A val list = listOf("A", "B", "C")
for (i in 0 until list.size step 2) { // 指定步长print("${list[i]} ")
}// A C 

10 不同场景推荐

  • 简单遍历:for-inforEach
  • 需要索引:forEachIndexedwithIndex
  • 逆序遍历:reversed() + for-in
  • 需要删除元素:使用 MutableIterator(在迭代中安全删除)
val list = listOf("A", "B", "C")
for (item in list.reversed()) {print("$item ")
}// C B A val mutableList = mutableListOf("A", "B", "C")
val iterator = mutableList.iterator()
while (iterator.hasNext()) {if (iterator.next() == "B") iterator.remove()
}
println(mutableList)// [A, C]
http://www.xdnf.cn/news/9725.html

相关文章:

  • Xshell连接Linux时出现Warning:The remote SSH server rejected X11 forwarding request.
  • Linux---系统守护systemd(System Daemon)
  • 江西某石灰石矿边坡自动化监测
  • 【Linux 基础知识系列】第二篇-Linux 发行版概述
  • LVS +Keepalived高可用群集
  • 设计模式-工厂方法模式
  • 【Python 进阶】抽象基类(Abstract Base Class)
  • 编译原理笔记 2025/4/22
  • 容器(如 Docker)中,通常不建议运行多个进程或要求进程必须运行在前台
  • Linux系列-2 Shell常用命令收集
  • fingerprint2浏览器指纹使用记录
  • Spring框架学习day4--Spring集成Mybatis(IOC)
  • Vue 3 的路由管理
  • 小白成长之路-Linux日志管理
  • [MMU]IOMMU的主要职能及详细的验证方案
  • 3 分钟学会使用 Puppeteer 将 HTML 转 PDF
  • Axure设计案例——科技感渐变线性图
  • 不可变集合类型转换异常
  • Astra学习之-如何修改Header-logo和favicon图标
  • Linux | Shell脚本的基础知识
  • 基于ubuntu安装hadoop
  • .NET8入门:14.ASP.NET Core MVC进阶——Model
  • 前端高频面试题1:HTML/CSS/浏览器/计算机网络
  • 安装 Node.js 和配置 cnpm 镜像源
  • Java异常处理的全面指南
  • 基于通义千问的儿童陪伴学习和成长的智能应用架构。
  • Spring AI 之对话记忆(Chat Memory)
  • [网页五子棋][匹配模块]处理开始匹配/停止匹配请求(匹配算法,匹配器的实现)
  • python h5py 读取mat文件的<HDF5 object reference> 问题
  • StarRocks x Iceberg:云原生湖仓分析技术揭秘与最佳实践