kotlin forEach循环return/break

kotlin forEach循环return/break

fun main(args: Array) {
    var a = mutableListOf("0", "1", "2", "3", "4")
    var b = mutableListOf()

    a.forEachIndexed { index, s ->
        if (index > 2) {
            return@forEachIndexed
        }

        b.add(s)
    }
    println(b)
    b.clear()


    a.forEachIndexed mybreak@{ index, s ->
        if (index > 3) {
            return@mybreak
        }

        b.add(s)
    }
    println(b)
}

 

[0, 1, 2]
[0, 1, 2, 3]

 

 

kotlin forEachIndexed arrayListOf<String>_zhangphil的博客-CSDN博客Python for循环中的zip_python zip函数用于for循环_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/131003571

 

你可能感兴趣的:(kotlin,kotlin)