kotlin 边for循环边删除

方法

直接倒序for循环,并删除

    var mutableListOf = mutableListOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")
    println(mutableListOf)
    for (index in mutableListOf.count() - 1 downTo 0) {
        if (index > 5) {
            mutableListOf.removeAt(index)
        }
    }
    println(mutableListOf)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[0, 1, 2, 3, 4, 5]
发布了167 篇原创文章 · 获赞 62 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/AdrianAndroid/article/details/103599961