kotlin forEach循环return/break

比眉伴天荒 2023-10-13 15:28 63阅读 0赞

kotlin forEach循环return/break

  1. fun main(args: Array<String>) {
  2. var a = mutableListOf("0", "1", "2", "3", "4")
  3. var b = mutableListOf<String>()
  4. a.forEachIndexed { index, s ->
  5. if (index > 2) {
  6. return@forEachIndexed
  7. }
  8. b.add(s)
  9. }
  10. println(b)
  11. b.clear()
  12. a.forEachIndexed mybreak@{ index, s ->
  13. if (index > 3) {
  14. return@mybreak
  15. }
  16. b.add(s)
  17. }
  18. println(b)
  19. }

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

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

发表评论

表情:
评论列表 (有 0 条评论,63人围观)

还没有评论,来说两句吧...

相关阅读