Kotlin for Android Developers
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
- Bu sahifa navigatsiya:
- 22.4 While and do/while loops
22.3 For loops
Though you won’t probably use them too much if you make use of functional operators in collections, for loops can be useful in some situations, so they are still available. It works with anything that provides an iterator: 1 for (item in collection) { 2 print(item) 3 } If we want to get a more typical iteration over indices, we can also do it using ranges (there are usually smarter solutions anyway): 1 for (index in 0..viewGroup.getChildCount() - 1) { 2 val view = viewGroup.getChildAt(index) 3 view.visibility = View.VISIBLE 4 } When iterating over an array or a list, a set of indices can be requested to the object, so the previous artifact is not necessary: 22. Flow control and ranges 101 1 for (i in array.indices) 2 print(array[i]) 22.4 While and do/while loops You can keep using while loops too, though it won’t be very common either. There are usually simpler and more visual ways to resolve a problem. A couple of examples: 1 while (x > 0) { 2 x-- 3 } 4 5 do { 6 val y = retrieveData() 7 } while (y != null) // y is visible here! 22.5 Ranges It’s difficult to explain flow control without talking about ranges. But their scope is much wider. Range expressions make use of an operator in the form of “ .. ” that is defined implementing a RangeTo function. Ranges help simplify our code in many creative ways. For instance we can convert this: 1 if (i >= 0 && i <= 10) 2 println(i) Into this: 1 if (i in 0..10) 2 println(i) Range is defined by any type that can be compared, but for numerical types the compiler will optimise it by converting it to simpler analogue code in Java, to avoid the extra overhead. The numerical ranges can also be iterated, and the loops are optimised too by converting them to the same bytecode a for with indices would use in Java: 22. Flow control and ranges 102 1 for (i in 0..10) 2 println(i) Ranges are incremental by default, so something like: 1 for (i in 10..0) 2 println(i) Would do nothing. You can, however, use the function downTo : 1 for (i in 10 downTo 0) 2 println(i) We can define a spacing different from 1 among the values in a range by using step : 1 for (i in 1..4 step 2) println(i) 2 3 for (i in 4 downTo 1 step 2) println(i) If you want to create an open range (which excludes the last item), you can use the function until : 1 for (i in 0 until 4) println(i) This previous line will print from 0 to 3, but will skip the last value. This means that 0 until 4 == 0..3 . For iterations over lists it could be easier to understand if we use for (i in 0 until list.size) instead of for (i in 0..list.size - 1) . As mentioned before, there are really creative ways to use ranges. For instance, an easy way to get the list of View s inside a ViewGroup would be: 1 val views = (0 until viewGroup.childCount).map { viewGroup.getChildAt(it) } The mix of ranges and functional operators prevents from having to use an explicit loop to iterate over the collection, and the creation of an explicit list where we add the views. Everything is done in a single line. If you want to know more about how ranges are implemented and a lot more examples and useful information, you can go to Kotlin reference²⁵ . ²⁵ https://kotlinlang.org/docs/reference/ranges.html |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling