0
Help for in or forEach Kotlin
What is the difference and which is best between fun main(args: Array<String>) { var arr = arrayOf(1, 3, 5) for (i in arr) { println(i*4) } } and fun main(args: Array<String>) { var arr = arrayOf(1, 3, 5) arr.forEach { println(it * 4) } }
2 Antworten
0
In case if you want to traverse only the array. You can use both the loops(any one of it)
But usign for each u can neither use conditions like print odd numbers, nor you can reverse the array
0
In case use for each loop because it reduces the chances of errors