3.9. Kotlin cycle control

发布时间 : 2025-10-25 13:34:28 UTC      

Page Views: 10 views

3.9.1. For cycle #

for loop can provide an iterator for any iterator syntax isas follows:

for (item in collection) print(item) 

The body of the loop can be a code block:

for (item: Int in ints) { // …… } 

As mentioned above for you can loop through any object that provides aniterator.

If you want to traverse an array or an list you can do this:

for (i in array.indices) { print(array[i]) } 

Note that this “traversal on the interval” is compiled into an optimized implementation without creating additional objects.

Or you can use library functions withIndex :

for ((index, value) in array.withIndex()) { println("the element at $index is $value") } 

Example #

Iterate over the collection:

fun main(args: Array<String>) { val items = listOf("apple", "banana", "kiwi") for (item in items) { println(item) } for (index in items.indices) { println("item at $index is ${items[index]}") } } 

Output result:

apple banana kiwi item at 0 is apple item at 1 is banana item at 2 is kiwi 

3.9.2. While and do…while cycle #

while is the most basic loop, and its structure is:

While (Boolean expression) { //Circular Content } 

do…while loop for while statement, you cannot enter a loop if the condition is not met. But sometimes we need to execute at least once, even if the conditions are not met.

do…while cyclic and while cycle is similar, except that do…while loop is executed at least once.

do { //code statement }While (Boolean expression); 

Example #

fun main(args: Array<String>) { println("----while use-----") var x = 5 while (x > 0) { println( x--) } println("----do...while use-----") var y = 5 do { println(y--) } while(y>0) } 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。