【问题标题】:why mutableSet does not update !! for item in list in Kotlin为什么 mutableSet 不更新!用于 Kotlin 列表中的项目
【发布时间】:2019-11-14 12:45:34
【问题描述】:

我希望 setList 为 1 到 10,但“currentNumber”(列表中的一项)不能下一个!!

var numList = mutableSetOf(1)
for (currentNumber in numList) {

    var temp = currentNumber
    temp++

    numList.add(temp)
    if (currentNumber == 10)
        break
}

println(" final List 0 to 10 $numList")

【问题讨论】:

  • 除了其他问题之外,调用一个集合'List'只是在积蓄麻烦......

标签: kotlin mutablelist


【解决方案1】:

这是因为您在迭代集合时正在修改集合。

您可以使用rangeTo 运算符创建一个数字序列,如下所示:

1..100 创建一个IntRange,您可以将其转换为列表或设置。

【讨论】:

  • 可能值得举个例子,包括括号:(1..100).toList()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 2015-06-25
  • 1970-01-01
相关资源
最近更新 更多