首页 > 解决方案 > Kotlin:是否可以重复使用 componentN 运算符?

问题描述

我正在从 hyperskill 学习 kotlin。

我很好奇是否可以将 componentN 运算符分配给重复功能,如下所示:

val (a, b, c) = repeat(3) { BigInteger(readLine()!!) }

我尝试使用componentN操作员扩展我的一些功能,但我不知道如何使用repeat

标签: kotlinrepeat

解决方案


repeat不返回任何内容,这意味着您不能使用此函数来解构声明。但是您可以使用Rangewithmap或:ListArray

val (a, b, c) = (1..3).map { BigInteger(readLine()!!) }
//or
val (a, b, c) = Array(3) { BigInteger(readLine()!!) }

推荐阅读