kotlin: for的用法之二

一,代码:

例子:

fun forExample2() {
    //until
    for (inex in 0 until 3){  // 不包含3
        println("until: $inex ")
    }
    // ..
    for (inex in 0 ..2){  // 包含2
        println("..: $inex ")
    }
    // downto
    for (inex in 2 downTo 0) {// 包含2和0
        println("downTo: $inex ")
    }
    // step
    for (index in 1 .. 20 step 2){ // step 2代表遍历的步长
        println("step: $index ")
    }

}

调用:

        //处理按钮点击事件
        binding.button1.setOnClickListener {
            forExample2()
        }

二,运行结果:

image

posted @ 2025-08-09 13:40  刘宏缔的架构森林  阅读(5)  评论(0)    收藏  举报