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

相关推荐
starrycode88814 小时前
【每日一个知识点】Kotlin基础语法核心学习笔记
笔记·学习·kotlin
alexhilton1 天前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
用户69371750013841 天前
29.Kotlin 类型系统:智能转换:类型检查 (is) 与类型转换 (as)
android·后端·kotlin
用户69371750013841 天前
30. Kotlin 扩展:为“老类”添“新衣”:扩展函数与扩展属性
android·后端·kotlin
ForteScarlet1 天前
如何解决 Kotlin/Native 在 Windows 下 main 函数的 args 乱码?
开发语言·windows·kotlin
starrycode8881 天前
【每日一个知识点】Kotlin开发基础知识
ui·kotlin
愤怒的代码2 天前
深入理解 IdleHandler:从启动优化到内存管理
android·架构·kotlin
Kapaseker2 天前
一万四千字重温 Android 四大组件
android·kotlin
我爱烤冷面2 天前
kotlin项目实现Java doc的方案:使用Dokka
java·开发语言·kotlin·dokka
jian110582 天前
android java转kotlin,kotlin转java
android·java·kotlin