本文概述
- Kotlin rangeTo()
- Kotlin downTo()
- Kotlin反转()
- Kotlin步骤()
- rangeTo()
- 向下()
- reversed()
- 步()
rangeTo()函数示例
fun main(args: Array<
String>
) {var range: IntRange = 1.rangeTo(5)println("Printing value: 1.rangeTo(5)")for (x in range){print("$x ")}println("")var range2: IntRange = IntRange(1, 5)println("Printing value: IntRange(1, 5)")for (x in range2){print("$x ")}}
输出:
Printing value: 1.rangeTo(5)1 2 3 4 5 Printing value: IntRange(1, 5)1 2 3 4 5
像Double, Float这样的浮点数据类型(或多个类型)未在rangeTo运算符中定义。
Kotlin downTo()downTo()扩展函数用于按降序从高阶到低阶的顺序返回值。 downTo()函数是为一对整数类型定义的。
句法:
fun Long.downTo(other: Int): LongProgression {return LongProgression.fromClosedRange(this, other.toLong(), -1L)}fun Byte.downTo(other: Int): IntProgression {return IntProgression.fromClosedRange(this.toInt(), other, -1)}
downTo()函数示例
fun main(args: Array<
String>
) {println("Range 1")var range1 = 5 downTo 1for (x in range1){print("$x ")}println()println("Range 2")var range2: IntProgression = 5.downTo(1)for (x in range2){print("$x ")}println()println("Range 3")var range3: IntProgression = IntProgression.fromClosedRange(5, 1, -1)for (x in range3){print("$x ")}}
输出:
Range 15 4 3 2 1 Range 25 4 3 2 1 Range 35 4 3 2 1
Kotlin反转()reversed()函数用于返回给定范围类型的相反顺序。
句法:
fun IntProgression.reversed(): IntProgression {return IntProgression.fromClosedRange(last, first, -step)}
reversed()函数的示例
fun main(args: Array<
String>
) {println("Reversed 1")var range1 = 1..5for (x in range1.reversed()){print("$x ")}println()println("Reversed 2")var range2: IntRange = IntRange(1, 5)for (x in range2.reversed()){print("$x ")}println()println("Reversed 3")var range3 = IntProgression.fromClosedRange(5, 1, -1)for (x in range3.reversed()){print("$x ")}println()println("Reversed 4")var range4: IntProgression = IntProgression.fromClosedRange(5, 1, -2)for (x in range4.reversed()){print("$x ")}}
输出:
Reversed 15 4 3 2 1 Reversed 25 4 3 2 1 Reversed 31 2 3 4 5 Reversed 41 3 5
Kotlin步骤()step()函数(或step运算符)用于以给定step值的间隔返回范围值。步长值始终为正参数。负步进值将生成IllegalArgumentException异常。
step()函数的示例
fun main(args: Array<
String>
) {val range: IntRange = 1..10println("Print range value with step 2:")for(x in range step (2)){print("$x ")}println("")println("Print range value with step 3:")for(x in range step 3){print("$x ")}val first=((range step 2).first)val last=((range step 2).last)println("")println("First value of interval: $first")println("Last value of interval: $last ")}
输出:
Print range value with step 2:1 3 5 7 9 Print range value with step 3:1 4 7 10 First value of interval: 1Last value of interval: 9
如果我们将步进值提供为负整数, 则会引发异常。
fun main(args: Array<
String>
) {val range: IntRange = IntRange(1, 10)for (x in range step -2){print("$x ")}}
【Kotlin实用程序功能】输出:
Exception in thread "main" java.lang.IllegalArgumentException: Step must be positive, was: -2. at kotlin.ranges.RangesKt__RangesKt.checkStepIsPositive(Ranges.kt:130) at kotlin.ranges.RangesKt___RangesKt.step(_Ranges.kt:432) at TestKt.main(Test.kt:63)
推荐阅读
- Kotlin变量
- Java互操作性(从Java调用Kotlin代码)
- Java互操作性
- Kotlin不安全和安全的转换操作符
- Kotlin类型转换
- Kotlin教程介绍
- Kotlin try-catch语句
- Kotlin抛出关键字throw
- Kotlin字符串