Kotlin return与return@forEachIndexed

Kotlin return与return@forEachIndexed

Kotlin 复制代码
fun main() {
    val data = arrayOf(0, 1, 2, 3, 4)

    println("a")

    data.forEachIndexed { index, v ->
        if (v == 2) {
            //类似while循环中的continue
            //跳过,继续下一个forEachIndexed迭代
            return@forEachIndexed
        }

        println("a index=$index $v")
    }

    println("b")

    data.forEachIndexed { index, v ->
        if (v == 2) {
            //这是退出整个main函数。
            return
        }

        println("b index=$index $v")
    }

    println("这里运行不到")
}

a

a index=0 0

a index=1 1

a index=3 3

a index=4 4

b

b index=0 0

b index=1 1

Process finished with exit code 0

kotlin forEachIndexed arrayListOf<String>_kotlin arraylist foreach-CSDN博客文章浏览阅读184次。Python for循环中的zip_python zip函数用于for循环_zhangphil的博客-CSDN博客。_kotlin arraylist foreachhttps://blog.csdn.net/zhangphil/article/details/131003571kotlin forEach循环return/break_kotlin foreach return-CSDN博客文章浏览阅读1.3k次。该文章通过代码示例展示了在Kotlin中如何在forEachIndexed循环中使用return和自定义标签的break,以及其对循环控制的影响。示例解释了如何在遇到特定条件时提前退出循环并添加元素到另一个列表。https://blog.csdn.net/zhangphil/article/details/131659989

相关推荐
ai2work5 天前
ch23 综合复刻:从零做一个最小可用版本(capstone)
kotlin
ai2work5 天前
ch21 签名、校验与发版
kotlin
JMchen5 天前
属性动画原理与高级动画实现
android·kotlin·canvas
Android打工仔5 天前
Kotlin 协程源码解析:协程是如何切换线程的?
android·kotlin
ai2work5 天前
附录 D 速查索引与阅读指南
kotlin
ai2work5 天前
ch22 诊断方法论:三个真实事故的根因分析
kotlin
Kapaseker5 天前
你有搞明白 Volatile 什么意思吗?
android·kotlin
alexhilton5 天前
藏在设备上的秘密,终究藏不住
android·kotlin·android jetpack
ai2work6 天前
ch15 加载模型与初始化上下文
kotlin
hai_android6 天前
Kotlin / Android 常用函数使用示例手册
android·java·kotlin