Kotlin 简明教程

Kotlin - Ranges

Kotlin range 由其两个端点值定义,这两个值都包含在范围内。Kotlin 范围是使用 rangeTo() 函数创建的,也可以简单地使用 downTo(. .) 运算符。范围内最主要的操作是 contains ,它通常用 in!in 运算符的形式。

Kotlin range is defined by its two endpoint values which are both included in the range. Kotlin ranges are created with rangeTo() function, or simply using downTo or (. .) operators. The main operation on ranges is contains, which is usually used in the form of in and !in operators.

Example

1..10  // Range of integers starting from 1 to 10

a..z   // Range of characters starting from a to z

A..Z   // Range of capital characters starting from A to Z

范围的两端总是包含在范围内,这意味着 1..4 表达式对应于值 1、2、3 和 4。

Both the ends of the range are always included in the range which means that the 1..4 expression corresponds to the values 1,2,3 and 4.

Creating Ranges using rangeTo()

要创建 Kotlin 范围,我们对范围起始值调用 rangeTo() 函数,并提供结束值作为参数。

To create a Kotlin range we call rangeTo() function on the range start value and provide the end value as an argument.

Example

fun main(args: Array<String>) {
   for ( num in 1.rangeTo(4) ) {
     println(num)
   }
}

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

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

1
2
3
4

Creating Ranges using .. Operator

rangeTo() 经常以其运算符形式 .. 调用。所以上面的代码可以使用 .. 运算符重写如下所示:

The rangeTo() is often called in its operator form ... So the above code can be re-written using .. operator as follows:

Example

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

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

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

1
2
3
4

Creating Ranges using downTo() Operator

如果我们想定义一个向后范围,可以使用 downTo 运算符:

If we want to define a backward range we can use the downTo operator:

Example

fun main(args: Array<String>) {
   for ( num in 4 downTo 1 ) {
     println(num)
   }
}

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

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

4
3
2
1

Kotlin step() Function

我们可以使用 step() 函数定义范围内值之间的距离。让我们看看以下示例:

We can use step() function to define the distance between the values of the range. Let’s have a look at the following example:

Example

fun main(args: Array<String>) {
   for ( num in 1..10 step 2 ) {
     println(num)
   }
}

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

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

1
3
5
7
9

Kotlin range of Characters

可以像为整数值创建范围那样为字符创建范围。

Ranges can be created for characters like we have created them for integer values.

Example

fun main(args: Array<String>) {
   for ( ch in 'a'..'d' ) {
     println(ch)
   }
}

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

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

a
b
c
d

Kotlin reversed() Function

函数 reversed() 可以用于颠倒一个范围的值。

The function reversed() can be used to reverse the values of a range.

Example

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

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

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

5
4
3
2
1

Kotlin until() Function

函数 until() 可以用于创建一个范围,但它会跳过给定的最后一个元素。

The function until() can be used to create a range but it will skip the last element given.

Example

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

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

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

1
2
3
4

The last, first, step Elements

我们可以使用 firstlaststep 范围属性来查找范围的第一个值、最后一个值或步长。

We can use first, last and step properties of a range to find the first, the last value or the step of a range.

Example

fun main(args: Array<String>) {
   println((5..10).first)
   println((5..10 step 2).step)
   println((5..10).reversed().last)
}

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

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

5
2
5

Filtering Ranges

filter() 函数将返回一个与给定谓词匹配的元素列表:

The filter() function will return a list of elements matching a given predicate:

Example

fun main(args: Array<String>) {
   val a = 1..10
   val f = a.filter { T -> T % 2 == 0 }

   println(f)
}

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

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

[2, 4, 6, 8, 10]

Distinct Values in a Range

distinct() 函数将从包含重复值的范围内返回一个不重复的值的列表:

The distinct() function will return a list of distinct values from a range having repeated values:

Example

fun main(args: Array<String>) {
   val a = listOf(1, 1, 2, 4, 4, 6, 10)

   println(a.distinct())
}

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

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

[1, 2, 4, 6, 10]

Range Utility Functions

我们还可以对范围应用许多其他有用的函数,例如 minmaxsumaveragecount

There are many other useful functions we can apply to our range, like min, max, sum, average, count:

Example

fun main(args: Array<String>) {
   val a = 1..10

   println(a.min())
   println(a.max())
   println(a.sum())
   println(a.average())
   println(a.count())
}

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

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

1
10
55
5.5
10