본문
170417(월) - Kotlin docs (Basic Syntax)
Kotlin docs
Basic Syntax
for loop
- Iterator를 사용한 synteax
fun main(args: Array<String>) {
for (arg in args) {
print(arg)
}
}
for (i in args.indices) {
print(args[i]) {
}
- withIndex
for ((index, value) in array.withIndex()) {
print($index is $value)
}
while loop
fun main(args: Array<String>) {
var i = 0
while (i < args.size) {
pirnt(args[i++])
}
}
do {
val y = retrieveData()
} while (y != null)
when expression
fun cases(obj: Any) {
when (obj) {
1 -> print("One")
0, 1 -> print("x == 0 or x == 1")
"Hello" -> print("Greeting")
parseInt(s) -> print("s encodes x")
in 1..10 -> print("x is in the range")
!in 1..10 -> print("x is outside range")
in validNumbers -> print(x is valid")
is Long -> print("Long")
!is Long -> print("Not long")
else -> print("Unknown")
}
}
val hasPrefix = when(x) {
is String -> x.startsWith("prefix")
else -> false
}
ranges
if (x in 1..y-1) {
print("OK")
}
if (x !in 0..array.lastIndex) {
print("Out")
}
for (x in 1..5) {
print(x)
}
- rangeTo()
Don't Understand!
rangeTo(ClosedRange<T>)
인 것 같다.
collections
for (name in names) {
println(name)
}
if (text in names) { // name.contains(text) is called
print("Yes")
}
- lambda expressions
names
.filter { it.startsWith("A") }
.sortedBy { it }
.map ( it.toUpperCase() }
.forEach { print(it) }
'Mobile > Kotlin' 카테고리의 다른 글
170607(수) - Kotlin docs (Basic Types) (0) | 2017.05.31 |
---|---|
170530(화) - Kotlin docs(Coding Conventions) (0) | 2017.05.30 |
170529(월) - Kotlin docs (Idioms) (0) | 2017.05.29 |
170410(월) - Kotiln-docs (Basic Syntax) (0) | 2017.04.10 |
161118(금) (0) | 2016.11.18 |
댓글