CS Electrical And Electronics
@cselectricalandelectronics

What is Index method in Kotlin?

All QuestionsCategory: KotlinWhat is Index method in Kotlin?
chetan shidling asked 3 years ago

I need short information.

1 Answers
chetan shidling answered 3 years ago

The index method is used to print both value and index numbers in an array. Let’s see one example.
 
Code:
 
fun main(args : Array<String>){
var nums = listOf(1,2,3,4)

for((i,e) in nums.withIndex())
{
println(” $i : $e”)
}
}
 
This code prints both Index and value.
 
Output:
 
0 : 1
1 : 2
2 : 3
3 : 4