この文書の現在のバージョンと選択したバージョンの差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
|
kotlin:kotlinコードサンプル [2019/08/31 22:25] ips |
kotlin:kotlinコードサンプル [2019/09/01 16:06] (現在) ips |
||
|---|---|---|---|
| ライン 26: | ライン 26: | ||
| } | } | ||
| } | } | ||
| + | </code> | ||
| + | ===== ifの書き方 ===== | ||
| + | <code> | ||
| + | fun maxOf(a: Int, b: Int): Int { | ||
| + | if (a > b) { | ||
| + | return a | ||
| + | } else { | ||
| + | return b | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | ↓同じ | ||
| + | <code> | ||
| + | fun main() { | ||
| + | println("max of 0 and 42 is ${maxOf(0, 42)}") | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ===== indices ===== | ||
| + | |||
| + | <code> | ||
| + | val list = listOf("a", "b", "c") | ||
| + | |||
| + | println(list.lastIndex) | ||
| + | println(list.indices) | ||
| + | ↓ | ||
| + | 2 | ||
| + | 0..2 | ||
| + | </code> | ||
| + | |||
| + | ===== for while ===== | ||
| + | |||
| + | <code> | ||
| + | fun main() { | ||
| + | val items = listOf("apple", "banana", "kiwifruit") | ||
| + | | ||
| + | for (index in items.indices) { | ||
| + | println("item at $index is ${items[index]}") | ||
| + | } | ||
| + | | ||
| + | for (item in items) { | ||
| + | println(item) | ||
| + | } | ||
| + | var index = 0 | ||
| + | while (index < items.size) { | ||
| + | println("item at $index is ${items[index]}") | ||
| + | index++ | ||
| + | } | ||
| + | ↓ | ||
| + | item at 0 is apple | ||
| + | item at 1 is banana | ||
| + | item at 2 is kiwifruit | ||
| + | apple | ||
| + | banana | ||
| + | kiwifruit | ||
| + | item at 0 is apple | ||
| + | item at 1 is banana | ||
| + | item at 2 is kiwifruit | ||
| + | } | ||
| + | ↓ | ||
| + | item at 0 is apple | ||
| + | item at 1 is banana | ||
| + | item at 2 is kiwifruit | ||
| + | apple | ||
| + | banana | ||
| + | kiwifruit | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | fun describe(obj: Any): String = | ||
| + | when (obj) { | ||
| + | 1 -> "One" | ||
| + | "Hello" -> "Greeting" | ||
| + | is Long -> "Long" | ||
| + | !is String -> "Not a string" | ||
| + | else -> "Unknown" | ||
| + | } | ||
| + | |||
| + | fun main() { | ||
| + | println(describe(1)) | ||
| + | println(describe("Hello")) | ||
| + | println(describe(1000L)) | ||
| + | println(describe(2)) | ||
| + | println(describe("other")) | ||
| + | ↓ | ||
| + | One | ||
| + | Greeting | ||
| + | Long | ||
| + | Not a string | ||
| + | Unknown | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ===== ラムダ式 フィルター マップコレクション ===== | ||
| + | <code> | ||
| + | val fruits = listOf("banana", "avocado", "apple", "kiwifruit") | ||
| + | fruits | ||
| + | .filter { it.startsWith("a") } | ||
| + | .sortedBy { it } | ||
| + | .map { it.toUpperCase() } | ||
| + | .forEach { println(it) } | ||
| + | ↓ | ||
| + | APPLE | ||
| + | AVOCADO | ||
| + | </code> | ||
| + | |||
| + | ===== Getter/Setter ===== | ||
| + | |||
| + | 自動でGetter Setter が作られる | ||
| + | <code> | ||
| + | class Data(var param: Int){ | ||
| + | |||
| + | } | ||
| + | |||
| + | fun main() { | ||
| + | val data = Data(5) | ||
| + | println(data.param) // called getter | ||
| + | data.param = 1 // called setter | ||
| + | println(data.param) // called getter | ||
| + | } | ||
| + | ↓ | ||
| + | 5 | ||
| + | 1 | ||
| + | </code> | ||
| + | |||
| + | Getter Setter をカスタマイズする場合 | ||
| + | |||
| + | <code> | ||
| + | class Animal(){ | ||
| + | var name:String = "" | ||
| + | var hello:String | ||
| + | get(){ | ||
| + | return name | ||
| + | } | ||
| + | set(value){ | ||
| + | name="${value} Chan" | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | fun main() { | ||
| + | val cat = Animal() | ||
| + | cat.hello = "HaNa" | ||
| + | println(cat.name) // called getter | ||
| + | |||
| + | } | ||
| + | ↓ | ||
| + | HaNa Chan | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== list ===== | ||
| + | |||
| + | [[https://qiita.com/kiririnyo/items/aee905225902d096f7c0|Kotlin の Collection まとめ ~List編~]] | ||
| + | [[https://qiita.com/opengl-8080/items/36351dca891b6d9c9687|Kotlin のコレクション使い方メモ]] | ||
| + | |||
| + | 不変のリストは listOf | ||
| + | 可変のリストは mutableListOf | ||
| + | 文字列の結合joinToString | ||
| + | |||
| + | |||
| + | <code> | ||
| + | fun joinOptions(options: Collection<String>) = options.joinToString(prefix = "[", postfix = "]") | ||
| + | |||
| + | fun main() { | ||
| + | val list = listOf("t", "e", "s", "t") | ||
| + | println(joinOptions(list)) | ||
| + | |||
| + | } | ||
| + | ↓ | ||
| + | [t, e, s, t] | ||
| + | </code> | ||
| - | </code> | ||