Kotlin教程:整数、字符类型范围例子、步长

2020-04-2408:20:41编程语言入门到精通Comments2,151 views字数 1754阅读模式

Kotlin范围定义为从起始值到结束值的间隔。 范围表达式使用运算符(..)创建,并由in!in补充。 等于或大于起始值且小于或等于结束值的值在定义的范围内。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

val aToZ = 'a'..'z'  
val oneToNine = 1..9
Kotlin

在上述代码:val aToZ ='a'..'z',表示a包含在aToZ中,b包含在aToZ中,依此类推。 代码val oneToNine = 1..9表示1包含在oneToNine中,但oneToNine中不包含10文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

整数类型范围

整数类型范围(IntRangeLongRangeCharRange)是用于for循环。 编译器以简单的Java索引for循环模拟转换此整数类型。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

Kotlin范围的例子文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

fun main(args: Array<String>) {

    for (a in 1..5){
        print(a )
    }
    println()
    for(x in 'a'..'f'){
        print(x )
    }
    println()
    val range = 1.0..5.0
    println(range)
    println("3.14 in range is ${3.14 in range}")
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

12345
abcdef
1.0..5.0
3.14 in range is true
Shell

当尝试使用递减顺序迭代r范围时发生了什么?..运算符将不打印任何内容。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

 for (a in 5..1){
     print(a )// print nothing
 }
Kotlin

要按降序迭代元素,请使用标准库downTo()函数或downTo关键字。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

for (a in 5 downTo 1){  
    print(a )// 54321  
}
Kotlin

until范围文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

until()函数或until范围中的关键字用于排除最后一个元素。 它迭代范围从开始到结束时少于1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

for (a in 1 until 5){  
    print(a) // print 1234  
}
Kotlin

以上范围不包括5,它是从14迭代。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

Kotlin整数范围

下面来看看如何使用downTo()downTorangeTo()方法的整数范围的示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

fun main(args: Array<String>) {
    for (x in 1..5)
        print(x)
    println()
    for (x in 5 downTo 1)
        print(x)
    println()
    for (x in 1.rangeTo(5))
        print(x)
    println()
    for (x in 5.downTo(1))
        print(x)
    println()
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

12345
54321
12345
54321
Shell

Kotlin字符范围

使用char数据类型的Kotlin范围示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

fun main(args: Array<String>) {
    for(x in 'a'..'e')
    print("$x ")
    println()
    for (x in 'e' downTo 'a')
        print("$x ")
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

a b c d e 
e d c b a
Shell

Kotlin范围步长

范围内的Kotlin step关键字用于在给定步长值(int类型)的区间内迭代范围。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

fun main(args: Array<String>) {
    for (x in 1..10 step 2)
        print("$x ")
    println()
    for (x in 10 downTo 1 step 3)
        print("$x ")
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

1 3 5 7 9 
10 7 4 1
Shell

Kotlin范围迭代器

iterator()方法也可用于迭代范围值。 它使用hasNext()方法检查范围中的下一个元素,next()方法返回范围的下一个元素。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

fun main(args: Array<String>) {

    val chars = ('a'..'e')
    val it = chars.iterator()
    while (it.hasNext()) {
        val x = it.next()
        print("$x ")
    }
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

a b c d e

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-integer-range.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18306.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/18306.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定