1. while loop
kotlin's while and do while are consistent with Java syntax. Here's a brief look
while (condition) { //When the condition is true, the code body executes /*...*/ } do {//Execute unconditionally once, and then execute when the condition is true /*...*/ } while (condition)
2. for loop
The for loop exists only in one form, which is consistent with the java for each loop
Java
int[] data={1,2,3,4,5,6,7,8,9,10}; for(int i:data){ system.out,println("i"); }
Kotlin
for(i in 10){ print(i) }
3. Numerical iteration: interval and sequence
There is no regular loop in java in kotlin, so kotlin uses the concept of interval
Interval essence: the interval between two values. These two values are usually numbers: one is the start value and the other is the end value. Use
Example:
val oneToTen = 1..10
Note: the kotlin interval is closed, which means that the second value is always part of the interval
If you can iterate over all the values in an interval, such an interval is called a sequence
For example, use when without parameters to implement FizzBuzz:
Rule: replace any number that can be divided by three with fizz, and replace any number that can be divided by five with buzz. If a number is a multiplier of three and five at the same time, we call it "FizzBuzz".
fun fizzBuzz(i: Int) = when { i % 15 == 0 -> "FizzBuzz " //i can be divided by 15 and returns FizzBuzz. Just like in Java,% is a modulo operation i % 3 == 0 -> "Fizz " //i can be divided by 5 and returns Buzz i % 5 == 0 -> "Buzz " //i can be divided by 3 and returns Fizz else -> "$i " //Else returns the number itself } for (i in 1..100) { //Iterative integer range 1.. 100 print(fizzBuzz(i)) } //1 2 Fizz 4 Buzz Fizz 7 ...
4. Iterative map
As we mentioned, the most common case is that the for... in loop iterates over a set. This is the same as Java, so let's see how to iterate a map.
For example: binary representation of printed characters
val binaryReps = TreeMap<Char, String>()//TreeMap is used, so the keys are sorted for (c in 'A'..'F') { //Iterates characters from A to F with the range of characters val binary = Integer.toBinaryString(c.toInt()) //Convert ASCII encoding to binary binaryReps[c] = binary//Store values in the map with the c key } for ((letter, binary) in binaryReps) { //Iterate over a map and assign key value pairs to two variables println("$letter = $binary") }
As can be seen from the above code.. the syntax creation range is applicable not only to numbers, but also to characters.
The above code uses a trick to use the key to access and update the concise syntax of the map. You can use map[key] to read the value and use map[key]=value to set it without get ting and set.
The following code:
binaryReps[c] = binary
Equivalent to java:
binaryReps. put(c,binary)
5. Use in to check the members of sets and intervals
Use the in operator to check whether a value is in an interval or its inverse operation! In to check whether the value is not in this interval.
fun isLetter(c: Char) = c in 'a'..'z' || c in 'A'..'Z' fun isNotDigit(c: Char) = c !in '0'..'9' println(isLetter('q')) //true println(isNotDigit('x')) //true
In fact, c in 'a'..'z 'is equivalent to a < = C & & C < = Z
In and! The in operand can also be used in the when expression
fun recognize(c: Char) = when (c) { in '0'..'9' -> "It's a digit!" in 'a'..'z', in 'A'..'Z' -> "It's a letter!" else -> "I don't know..." } println(recognize('8')) //It's a digit!
The use of in is not limited to strings. If you have any class that supports instance comparison (implements java.lang.Comparable), you can compare instances of this class.
For example:
println("Kotlin" in "Java".."Scala") //Same as "Java" < = "Kotlin" & & "Kotlin" < = "Scala" //true
Strings are compared alphabetically here, because the String class implements the Comparable interface in this way.
in also applies to sets
println("Kotlin" in setOf("Java", "Scala", "hello") / / there is no "Kotlin" string in this set
//false
kotlin is not included in the set.
summary
1. For, while, and do while loops are similar to java, but for loops are now more convenient, especially when iterating over map s.
2. The interval is represented by concise syntax 1.. 5.
3. You can use in and! In operator to check whether a value belongs to an interval.
3. .. Contains the start and end values.
Related videos:
Android advanced system learning -- Gradle introduction and project practice_ Beep beep beep_ bilibili
Android network architecture construction and principle analysis (I) -- witness the growth of network modules step by step through a network request_ Beep beep beep_ bilibili
[advanced Android course] - explanation of colin Compose's drawing principle (I)_ Beep beep beep_ bilibili
[advanced Android tutorial] - Handler source code analysis for Framework interview_ Beep beep beep_ bilibili
[advanced Android tutorial] - Analysis of hot repair Principle_ Beep beep beep_ bilibili
[advanced Android tutorial] - how to solve OOM problems and analysis of LeakCanary principle_ Beep beep beep_ bilibili