Output
println()
1fun main() {
2 println("Hello, World!")
3}
Function
fun
1fun sum(x: Int, y: Int): Int {
2 return x + y
3}
4
5// Single-Expression
6fun mul(x: Int, y: Int) = x * y
7
8// Default Arguments
9fun printMSG (msg1: String = "Hello", msg2: String) {
10 println("$msg1, $msg2!")
11}
12
13fun main() {
14 println(sum(1, 2))
15 println(mul(2, 5))
16 printMSG("Hi", "Jack")
17 printMSG(msg2 = "Kelly")
18}
vararg
1fun sum(vararg a: Int): Int {
2 var ret = 0
3 for (x in a) ret = ret + x
4 return ret
5}
6
7fun main() {
8 println(sum(1, 0, -1, 2))
9}
Variable
val
var
val
一定要初始化
1fun main() {
2 val v = 1 // immutalbe
3 var x: Int // mutable
4 x = 1
5 println(x + v)
6}
null
- 用
?
代表 nullable
1fun main() {
2 var notNull: String = "Hi!"
3 // notNull = null // error
4 var canNull: String? = "Hello!"
5 canNull = null
6
7 fun len(s: String) = println(s.length)
8
9 len(notNull)
10 // len(canNull) // error
11 canNull = "OAO"
12 len(canNull)
13
14 fun nullLen(s: String?) {
15 var L:Int = 0
16 if (s != null) L = s.length
17 println(L)
18 }
19
20 canNull = null
21 nullLen(canNull)
22}
Flow Control
when
類似 c++ 中的 switch。
1fun cases(obj: Any) {
2 when (obj) {
3 1 -> println("One")
4 is String -> println("String")
5 else -> println("Else")
6 }
7}
8
9fun main() {
10 cases(1)
11 cases(1.0)
12 cases("a")
13}
for
1fun main() {
2 val L = listOf("apple", "banana", "orange")
3 for (x in L) {
4 println("I like $x.")
5 }
6}
1fun main() {
2 for (i in 0..3) print(i) //0123
3 println("")
4 for (i in 0..8 step 2) print(i) //02468
5 println("")
6 for (i in 3 downTo 0) print(i) //3210
7 println("")
8 for (i in 8 downTo 0 step 2) print(i) //86420
9 println("")
10}
while
1fun main() {
2 var i = 0
3 var sum = 0
4 while (i < 5) {
5 sum += i
6 i++
7 }
8 println(sum)
9}
if
1fun main() {
2 val x = 't'
3 if (x in 'a'..'e')
4 println("Good")
5 else
6 println("Bad")
7}
Collections
List
MutableList
,List
mutableListOf()
,listOf()
add()
getOrNull()
,getOrElse()
1fun main() {
2 val L: List<Int> = listOf(1, 2, 3)
3 var mL: MutableList<Int> = mutableListOf(0)
4 L.forEach {
5 x -> mL.add(x)
6 }
7 mL.forEach {
8 i -> println("elemetn: $i")
9 }
10 println("L[2] = ${L.getOrNull(2)}") // 3
11 println("L[3] = ${L.getOrNull(3)}") // null
12 println("L[10] = ${L.getOrElse(10, {it})}") // 10
13 println("${L.subList(1, 2)}") // [2]
14}
Search & Sort
sort()
,shuffle()
,reverse()
indexOf()
,lastIndexOf()
indexOfFirst()
,indexOfLast()
binarySearch()
fill()
1fun main() {
2 var mL: MutableList<Int> = mutableListOf(2, 1, 7, 5, 9, 1)
3 println(mL.indexOf(1)) // 1
4 println(mL.lastIndexOf(1)) // 5
5 mL.sort() // [1, 1, 2, 5, 7, 9]
6 println(mL.indexOf(1)) // 0
7 println(mL.lastIndexOf(1)) // 1
8 println(mL.indexOfFirst{ it > 5 }) // 4
9 println(mL.binarySearch(3)) // -4
10 println(mL.binarySearch(1, 1, 5)) // 1
11 mL.fill(0)
12 println(mL) // [0, 0, 0, 0, 0, 0]
13}
Set
Set
,MutableSet
setOf()
,mutableSetOf()
add()
: return Booleanintersect()
,union()
,subtract()
1fun main() {
2 val S1: Set<Int> = setOf(4, 3, 2, 1)
3 val S2: Set<Int> = setOf(1, 2, 3, 4)
4 println("Two sets are equal: ${S1 == S2}") // true
5 val S3: Set<Int> = setOf(0, 2, 4, 6)
6 println("S2 union S3: ${S2 union S3}") // [1, 2, 3, 4, 0, 6]
7 println("S2 intersect S3: ${S2 intersect S3}") // [2, 4]
8 println("S2 subtract S3: ${S2 subtract S3}") // [1, 3]
9 println("S3 subtract S2: ${S3 subtract S2}") // [0, 6]
10 println("S1 subtract S2: ${S1 subtract S2}") // []
11 var S4: MutableSet<Int> = mutableSetOf(1, 2, 3, 4)
12 println("${S4.add(0)}") // true
13 println("${S4.add(1)}") // false
14}
Map
1fun main() {
2 val map = mapOf("key" to 42);
3 println("${map["key"]}") // 42
4 println("${map["key2"]}") // null
5 println("${map.getValue("key")}") // 42
6}