官方文檔: http://kotlinlang.org/docs/reference/ranges.html
1.區(qū)間/范圍(Ranges)
區(qū)間/范圍(Ranges)表達(dá)式: 由操作符..(rangeTo函數(shù)), in 和 !in 構(gòu)成!
任何可比較的類型(comparable type)都定義了區(qū)間,但整型原生類型的區(qū)間實(shí)現(xiàn)有優(yōu)化!
1.區(qū)間的使用示例:
fun main(args: Array<String>) {
val i = 3
val array = listOf("a","b","c")
//等價(jià)于 if(1 <= i && i <= 10)
if (i in 1..10)
println("$i,位于[1,10]區(qū)間")
//區(qū)間操作符..的底層實(shí)現(xiàn)就是rangeTo()函數(shù)
if (i in 1.rangeTo(10))
println("$i,位于[1,10]區(qū)間")
//檢查數(shù)組的下標(biāo)索引是否越界
if (i !in 0..array.size - 1)
println("數(shù)組越界Out: array has only ${array.size}")
//檢查數(shù)組是否包含某元素
if ("b" in array) //相當(dāng)于 collection.contains(obj)
println("Yes")
}
2.整型區(qū)間-迭代(循環(huán)遍歷)
整型區(qū)間(IntRange,LongRange,CharRange)還有額外特性:可迭代(循環(huán)遍歷)
kotlin編譯器會(huì)將整型區(qū)間轉(zhuǎn)換為類似Java基于索引的for循環(huán),沒有額外開銷!
fun main(args: Array<String>) {
for (i in 1..4) print(i) //輸出“1234”
for (i in 4..1) print(i) //什么都不輸出
//倒序迭代(循環(huán)遍歷)數(shù)字區(qū)間,可用標(biāo)準(zhǔn)庫中downTo函數(shù)
for (i in 4 downTo 1) print(i) //輸出“4321”
//不以1的步長迭代(循環(huán)遍歷)數(shù)字區(qū)間,可用step函數(shù)
for (i in 1..4 step 2) print(i) //輸出“13”
for (i in 4 downTo 1 step 2) print(i) //輸出“42”
//不包括結(jié)束元素的數(shù)字區(qū)間,可用until函數(shù):
for (i in 1 until 10) //i in [1, 10)排除10
print(i)
}
2.區(qū)間的工作原理(How work)
區(qū)間實(shí)現(xiàn)了一個(gè)公共接口:ClosedRange<T>,表示一個(gè)數(shù)學(xué)意義上的閉區(qū)間,
有兩個(gè)端點(diǎn)start和endInclusive都包含在區(qū)間內(nèi),
主要操作是contains,通常以in/!in操作符形式使用!
區(qū)間操作符..創(chuàng)建同時(shí)實(shí)現(xiàn)ClosedRange<T>和xxProgression的xxRange對(duì)象,
例如, 區(qū)間IntRange對(duì)象實(shí)現(xiàn)了ClosedRange<Int>,并繼承了IntProgression,
downTo()和step()函數(shù)的結(jié)果總是IntProgression
整型數(shù)列(IntProgression,LongProgression,CharProgression)代表等差數(shù)列,
數(shù)列Progression由first元素,last元素和非零step元素構(gòu)成,
首元素是first,下一元素是上一元素加上step,如果數(shù)列非空,那么last元素總會(huì)被迭代命中
數(shù)列Progression由其伴生對(duì)象的fromClosedRange函數(shù)構(gòu)造初始化:
IntProgression.fromClosedRange(start, end, step)
數(shù)列Progression是Iterable<N>的子類(泛型N分別為Int,Long,Char),
可用于for循環(huán),map,filter等函數(shù),數(shù)列迭代相當(dāng)于Java的基于索引的for循環(huán):
for (int i = first; i != last; i += step) {
}
3.區(qū)間的常用函數(shù)(Utility functions)
1.rangeTo()函數(shù)
整型類型(Int,Long,Byte,Char)都定義rangeTo()操作符(..),就是調(diào)用xxRange類的構(gòu)造函數(shù):
class Int {
operator fun rangeTo(other: Long): LongRange = LongRange(this, other)
operator fun rangeTo(other: Int): IntRange = IntRange(this, other)
}
浮點(diǎn)類型(Double,Float)未定義rangeTo()操作符(..),
但是可用標(biāo)準(zhǔn)庫提供的Comparable泛型的rangeTo()函數(shù):
//該函數(shù)返回的區(qū)間ClosedRange不能用于迭代(循環(huán)遍歷)
public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T>
2.downTo()函數(shù)
整型類型(Int,Long,Byte,Char)都定義擴(kuò)展函數(shù)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)
}
3.reversed()函數(shù)
數(shù)列xxProgression類定義了擴(kuò)展函數(shù)reversed(),用于反轉(zhuǎn)(逆序)數(shù)列:
fun IntProgression.reversed(): IntProgression {
return IntProgression.fromClosedRange(last, first, -step)
}
4.step()函數(shù)
數(shù)列xxProgression類定義了擴(kuò)展函數(shù)step(),用于修改數(shù)列的step值,
步長step值始終為正,因此不會(huì)更改迭代方向:
fun IntProgression.step(step: Int): IntProgression {
if (step <= 0) throw IllegalArgumentException("Step must be positive, was: $step")
return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
fun CharProgression.step(step: Int): CharProgression {
if (step <= 0) throw IllegalArgumentException("Step must be positive, was: $step")
return CharProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
注意,step函數(shù)返回的數(shù)列l(wèi)ast值可能與原始數(shù)列的last值不同,例如:
(1..12 step 2).last == 11 //數(shù)列為[1, 3, 5, 7, 9, 11]
(1..12 step 3).last == 10 //數(shù)列為[1, 4, 7, 10]
(1..12 step 4).last == 9 //數(shù)列為[1, 5, 9]
簡(jiǎn)書:http://www.reibang.com/p/c94879a596e8
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/74160747
GitHub博客:http://lioil.win/2017/07/02/Kotlin-ranges.html
Coding博客:http://c.lioil.win/2017/07/02/Kotlin-ranges.html