1.区间的定义
fun main(args: Array<String>) {
//三种定义方式
val range1 = 1..100
val range2 = IntRange(1, 100)
val range3 = 1.rangeTo(100)
/*-------长整型区间----------------*/
val range4 = 1L..100L
val range5 = LongRange(1L, 100L)
val ramge6 = 1L.rangeTo(100L)
/*-------字符区间----------------*/
val range7 = 'a'..'z'
val range8 = CharRange('a', 'z')
val range9 = 'a'.rangeTo('z')
}
2.反向区间的定义
fun main(args: Array<String>) {
val range=100 downTo 1
range.forEach{
println(it)
}
/*-------区间反转----------------*/
val reversed = range.reversed()
reversed.forEach{
println(it)
}
//指定步长
for (value in reversed step 2){
println(value)
}
}
3.区间的遍历
fun main(args: Array<String>) {
//三种定义方式
val range1 = 1..100
val range2 = IntRange(1, 100)
val range3 = 1.rangeTo(100)
/*-------长整型区间----------------*/
val range4 = 1L..100L
val range5 = LongRange(1L, 100L)
val ramge6 = 1L.rangeTo(100L)
/*-------字符区间----------------*/
val range7 = 'a'..'z'
val range8 = CharRange('a', 'z')
val range9 = 'a'.rangeTo('z')
for (index in range1) {
println(index)
}
for ((index, value) in range4.withIndex()) {
"index=$index value=$value"
}
range7.forEach {
println(it)
}
}
网友评论