The code below shows a typical way to show both index and value in many languages, including Kotlin. Which line of code shows a way to get both index and value more idiomatically?
var ndx = 0;
for (value in 1..5){
println("$ndx - $value")
ndx++
}
var ndx = 0;
for (value in 1..5){
println("$ndx - $value")
ndx++
}A. for( (ndx, value) in (1..20).withIndex() ){
B. for( (ndx, value) in (1..20).pair() ){
C. for( Pair(ndx, value) in 1..20 ){
D. for( (ndx, value) in *(1..20) ){
Answer: Option A

Join The Discussion