2019年4月20日土曜日

開発環境

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 9(collections - Get Organized)、Sharpen your pencil(273)の解答を求めてみる。

コード

fun main() {
    val petsLiam = listOf("Cat", "Dog", "Fish", "Fish")
    val petsSophia = listOf("Cat", "Owl")
    val petsMoah = listOf("Dog", "Dove", "Dog", "Dove")
    val petsEmily = listOf("Hedgehog")

    val pets = mutableListOf<String>()

    for (item in arrayOf(petsLiam, petsSophia, petsMoah, petsEmily)) {
        pets.addAll(item)
    }

    println(pets)
    println(pets.size)
    println(pets.toSet().size)
    println(pets.toSet().toList().sorted())
}

入出力結果

[Cat, Dog, Fish, Fish, Cat, Owl, Dog, Dove, Dog, Dove, Hedgehog]
11
6
[Cat, Dog, Dove, Fish, Hedgehog, Owl]

Process finished with exit code 0

0 コメント:

コメントを投稿