How to get the current index in for each Kotlin

前端 未结 7 1285
Happy的楠姐
Happy的楠姐 2021-01-30 01:51

How to get the index in a for each loop? I want to print numbers for every second iteration

For example

for (value in collection) {
    if (iteration_no %          


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 02:35

    Working Example of forEachIndexed in Android

    Iterate with Index

    itemList.forEachIndexed{index, item -> 
    println("index = $index, item = $item ")
    }
    

    Update List using Index

    itemList.forEachIndexed{ index, item -> item.isSelected= position==index}
    

提交回复
热议问题