Android 知识

Kotlin内置函数- 合并 zip

2022-05-06  本文已影响0人  zcwfeng
    val colors = listOf("red", "brown", "grey")
    val animals = listOf("fox", "bear", "wolf")
    println(colors zip animals)

    val twoAnimals = listOf("fox", "bear")
    println(colors.zip(twoAnimals))

------>
[(red, fox), (brown, bear), (grey, wolf)]
[(red, fox), (brown, bear)]

val colors = listOf("red", "brown", "grey")
val animals = listOf("fox", "bear", "wolf")

println(colors.zip(animals) { color, animal -> "The ${animal.capitalize()} is $color"})

------>
[The Fox is red, The Bear is brown, The Wolf is grey]

要分割键值对列表,请调用 unzip()

val numberPairs = listOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(numberPairs.unzip())

------>
([one, two, three, four], [1, 2, 3, 4])

上一篇下一篇

猜你喜欢

热点阅读