Kotlin For Each Loop Code Example


Example 1: for loop kotlin


val array = arrayOf(1, 3, 9)
for (item in array)
{
//loops items
}
for (index in 0..array.size - 1) {
//loops all indices
}
for (index in 0 untill array.size) {
//loops all indices
}
for (index in array.indices) {
//loops all indices (performs just as well as two examples above)
}

Example 2: kotlin for loop


val list = listOf("A", "B", "C")
for (element in list)
{
println(element)
}

Example 3: kotlin iterate array


val names = listOf("Anne", "Peter", "Jeff")
for (name in names)
{
println(name)
}

Example 4: for loop kotlin


for (x in 0..10) println(x)

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android SDK Location Should Not Contain Whitespace, As This Cause Problems With NDK Tools