Kotlin学习之Range

一.Range介绍

range即范围,定义方法如下

    val range1:IntRange =0..500//全闭区间
    val range2:IntRange =0 until 500//左闭右开区间

判断是否在某个范围内

    println(500 in range1)//true
    println(500 in range2)//false
    println(range1.contains(1))
/*

public class IntRange(start: Int, endInclusive: Int) : IntProgression(start, endInclusive, 1), ClosedRange {
    override val start: Int get() = first
    override val endInclusive: Int get() = last

    override fun contains(value: Int): Boolean = first <= value && value <= last

    override fun isEmpty(): Boolean = first > last

    override fun equals(other: Any?): Boolean =
        other is IntRange && (isEmpty() && other.isEmpty() ||
        first == other.first && last == other.last)

    override fun hashCode(): Int =
        if (isEmpty()) -1 else (31 * first + last)

    override fun toString(): String = "$first..$last"

    companion object {
        /** An empty range of values of type Int. */
        public val EMPTY: IntRange = IntRange(1, 0)
    }
}

*/

好玩的CharRange
定义方法及判方法

    val char1:CharRange = '欣'..'桑'
    println('t' in char1)//false
    println(char1.toString())//直接输出  欣..桑

还有LongRange

//    还有Long
    val long1:LongRange = 1..10000000000
//    位数少了人家还不愿意
    println(long1.contains(0))
//    false
    println(long1.contains(1))
//     true

大概就这些啦~

你可能感兴趣的:(Android)