June 2018
Intermediate to advanced
310 pages
6h 32m
English
The for loop in Java, which prints each character of a string on a new line, may look something like this:
final String word = "Word";for (int i = 0; i < word.length; i++) { }
The same loop in Kotlin is:
val word = "Word";for (i in 0..(word.length-1)) { println(word[i])}
Note that while the usual for loop in Java is exclusive (it excludes the last index by definition, unless specified otherwise), the for loop over ranges in Kotlin is inclusive. That's the reason we have to subtract one from the length to prevent overflow (string index out of range): (word.length-1).
If you want to avoid that, you can use the until function:
val word = "Word";for (i in 0 until word.length) { println(word[i])}
Unlike some other languages, reversing ...
Read now
Unlock full access