Kotlin 之 forEach 跳出循环

    技术2022-07-11  125

    return@forEach

    继续循环continue;

    (0..10).forEachIndexed { index, it ->         println("-- forEach -- ${index} --")         if (it > 5) return@forEachIndexed         println(it)       } 结果

    -- forEach -- 0 -- 0 -- forEach -- 1 -- 1 -- forEach -- 2 -- 2 -- forEach -- 3 -- 3 -- forEach -- 4 -- 4 -- forEach -- 5 -- 5 -- forEach -- 6 -- -- forEach -- 7 -- -- forEach -- 8 -- -- forEach -- 9 -- -- forEach -- 10 --  

     

    run outside@{}

    结束循环break;

    run outside@{         (0..10).forEachIndexed { index, it ->             println("-- forEach -- ${index} --")             if (it > 5) return@outside             println(it)         }     }

    结果

    -- forEach -- 0 -- 0 -- forEach -- 1 -- 1 -- forEach -- 2 -- 2 -- forEach -- 3 -- 3 -- forEach -- 4 -- 4 -- forEach -- 5 -- 5 -- forEach -- 6 --  

    Processed: 0.068, SQL: 9