Kotlin Range
Kotlinμ λ²μλ₯Ό μ½κ² μμ±ν μ μλ rangeTo()
ν¨μλ₯Ό μ 곡νλ€.
rangeTo()λ ..
μ€νΌλ μ΄ν°λ‘ ννλ κ°λ₯νλ€.
val i = 1
if (i in 1..4) { // equivalent of 1 <= i && i <= 4
print(i)
}
/*
>> μ€ν κ²°κ³Ό
1
*/
IntRange
, LongRange
, CharRange
λ λ°λ³΅λ μ§μνλ€.
for (i in 1..4) print(i)
λ°λ μμλ‘ λ°λ³΅νλ €λ©΄ ..
λμ downTo
λ₯Ό μ¬μ©νλ€.
for (i in 4 downTo 1) print(i)
λλ μ΄λ―Έ μ ν΄μ§ μμ΄μ reversed()
ν¨μλ₯Ό μ¬μ©νλ€.
for (i in (1..4).reversed()) print(i)
step
μ μ΄μ©νλ©΄ 1μ© μ¦κ°/κ°μκ° μλ μνλ μλ§νΌ 건λλΈ μ μλ€.
for (i in 1..8 step 2) print(i)
println()
for (i in 8 downTo 1 step 2) print(i)
λ§μ§λ§ μμλ₯Ό ν¬ν¨νμ§ μκ³ λ°λ³΅νλ κ²½μ°, until
μ μ¬μ©νλ€.
for (i in 1 until 10) { // i in [1, 10), 10μ ν¬ν¨λμ§ μμ
print(i)
}
/*
>> μ€ν κ²°κ³Ό
1
2
(μλ΅)
9
*/
Comments