Kotlin 简明教程

Kotlin - For Loop

What are loops?

想象一下,你需要在屏幕上打印一个句子20次。你可以通过20次使用print语句来实现。如果你需要打印相同的句子一千次呢?这时我们就需要使用循环来简化编程工作。实际上,循环在编程中用于重复特定的代码块,直到满足某些条件。

Imagine a situation when you need to print a sentence 20 times on your screen. You can do it by using print statement 20 times. What about if you need to print the same sentence one thousand times? This is where we need to use loops to simplify the programming job. Actually, Loops are used in programming to repeat a specific block of code until certain condition is met.

Kotlin支持多种类型的循环,在本章节,我们将学习Kotlin for 循环。

Kotlin supports various types of loops and in this chapter we are going to learn Kotlin for loop.

Kotlin For Loop

Kotlin for 循环迭代任何提供迭代器(即包含可计数数值的项)的内容,例如数组、范围、映射或Kotlin中可用的任何其他集合。Kotlin for 循环等效于C#等语言中的 foreach 循环。

Kotlin for loop iterates through anything that provides an iterator ie. that contains a countable number of values, for example arrays, ranges, maps or any other collection available in Kotlin. Kotlin for loop is equivalent to the foreach loop in languages like C#.

Syntax

Kotlin for 循环的语法如下:

The syntax of the Kotlin for loop is as follows:

for (item in collection) {
    // body of loop
}

Iterate Through a Range

我们将在单独的章节中学习Kotlin范围,现在你应该知道Kotlin范围提供迭代器,因此我们可以使用 for 循环迭代范围。

We will study Kotlin Ranges in a separate chapter, for now you should know that Kotlin Ranges provide iterator, so we can iterate through a range using for loop.

以下示例演示了循环如何遍历范围并打印各个项。为了遍历数字范围,我们将使用范围表达式:

Following is an example where the loop iterates through the range and prints individual item. To iterate over a range of numbers, we will use a range expression:

fun main(args: Array<String>) {
   for (item in 1..5) {
      println(item)
   }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

1
2
3
4
5

我们来看另一个示例,其中循环将遍历另一个范围,但这次它将向下递减而不是像上例中那样向上递增:

Let’s see one more example where the loop will iterate through another range, but this time it will step down instead of stepping up as in the above example:

fun main(args: Array<String>) {
   for (item in 5 downTo 1 step 2) {
      println(item)
   }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

5
3
1

Iterate Through a Array

Kotlin数组是提供迭代器的另一种数据类型,因此我们可以使用 for 循环以与处理范围相同的方式遍历Kotlin数组。

Kotlin Array is another data type which provides iterator, so we can use for loop to iterate through a Kotlin array in the similar way as we did it for the ranges.

以下示例演示了我们如何使用 for 循环遍历字符串数组:

Following is an example where we used for loop to iterate through an array of strings:

fun main(args: Array<String>) {
   var fruits = arrayOf("Orange", "Apple", "Mango", "Banana")

   for (item in fruits) {
      println(item)
   }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Orange
Apple
Mango
Banana

让我们再次看同样的示例,但这次我们将使用其索引遍历该数组。

Let’s see the same example once again, but this time we will iterate through the array using its index.

fun main(args: Array<String>) {
   var fruits = arrayOf("Orange", "Apple", "Mango", "Banana")

   for (index in fruits.indices) {
      println(fruits[index])
   }
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Orange
Apple
Mango
Banana