This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getStringLength(obj: Any): Int? { | |
if (obj !is String) return null | |
// objλ μλμΌλ‘ StringμΌλ‘ νλ³νλ©λλ€. | |
return obj.length | |
} | |
fun getStringLength(obj: Any): Int? { | |
// objλ && μ€λ₯Έμͺ½ ꡬ문λΆν° StringμΌλ‘ νλ³νλ©λλ€. | |
if (obj is String && obj.length > 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val items = listOf("apple", "banana", "kiwifruit") | |
for (item in items) { | |
println(item) | |
} | |
for (index in items.indices) { | |
println("μΈλ±μ€ $index λ²μ μλ νλͺ©μ ${items[index]} μ λλ€.") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val items = listOf("apple", "banana", "kiwifruit") | |
var index = 0 | |
while (index < items.size) { | |
println("item at $index is ${items[index]}") | |
index++ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun describe(obj: Any): String = | |
when (obj) { | |
1 -> "One" | |
"Hello" -> "Greeting" | |
is Long -> "Long" | |
!is String -> "Not a string" | |
else -> "Unknown" | |
} | |
fun main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val x = 10 | |
val y = 9 | |
if (x in 1..y+1) { | |
println("range μμ λ€μ΄κ°λ€μ") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val list = listOf("a", "b", "c") | |
if (-1 !in 0..list.lastIndex) { | |
println("-1μ λ²μ λ°μ μλ€μ") | |
} | |
if (list.size !in list.indices) { | |
println("리μ€νΈ μ¬μ΄μ¦κ° μΈλ±μ€ λ²μ λ°μ μλ€μ") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
for (x in 1..5) { | |
print(x) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
for (x in 1..10 step 2) { | |
print(x) //13579 | |
} | |
println() | |
for (x in 9 downTo 0 step 3) { | |
print(x) //9630 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val items = listOf("apple", "banana", "kiwifruit") | |
for (item in items) { | |
println(item) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val items = setOf("apple", "banana", "kiwifruit") | |
when { | |
"orange" in items -> println("juicy") | |
"apple" in items -> println("apple is fine too") | |
} | |
} |