我正在使用 removeAt 函数,它工作正常,但突然编译器开始抛出错误未解析的引用:removeAt
这是代码:
fun main() {
val nums = mutableListOf(-3, 167, 0, 9, 212, 3, 5, 665, 5, 8) // this is just a list
var newList = nums.sorted() // this is the sorted list
var finall = mutableListOf<Int>() // this is an empty list that will contain all the removed elements
for(i in 1..3) {
var min: Int = newList.removeAt(0) // element removed from newList is saved from min variable
// above line is producing the error
finall.add(min) // then the min variable is added to the empty list created before
}
println(finall)
println(newList)
}
我研究了一堆纪录片,但我找不到我的错误
第 3 行,您对列表进行排序,返回一个列表而不是 mutableList。所以 newList 是不可变的。替换第 3 行var newList = nums.sorted().toMutableList()
将使 newList 可变并解决您的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句