简体   繁体   English

如何使用 forEach 破坏 kotlin 嵌套对

[英]how destruct kotlin nested pairs with forEach

I need to destruct kotlin nested pairs.我需要破坏 kotlin 嵌套对。 How can do this simply without using pair.first/pair.second如何在不使用 pair.first/pair.second 的情况下简单地做到这一点

val chars = listOf('A', 'B', 'C')
val ints = listOf(1, 2, 3)
val booleans = listOf(true, false, false)

val cib: List<Pair<Pair<Char, Int>, Boolean>> = chars.zip(ints).zip(booleans)
cib.forEach { ((c, i), b) -> // compile error
    println("$c $i $b")
}

Not sure if there really is a way of desctructuring a Pair<Pair<*,*>> straight away, but you could do this:不确定是否真的有一种直接解构Pair<Pair<*,*>>的方法,但你可以这样做:

cib.forEach { (pair, b) -> 
    val (c, i) = pair
    //do stuff with c, i, b
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM