首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未解析的引用: removeAt()

未解析的引用: removeAt()
EN

Stack Overflow用户
提问于 2020-09-17 15:47:04
回答 3查看 291关注 0票数 0

我正在使用removeAt函数,它工作得很好,但是突然编译器开始抛出错误的未解析引用: removeAt

代码如下:

代码语言:javascript
运行
复制
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)

 
}

我研究了一大堆文档,但我找不到我的错误

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-09-17 15:56:29

第三行是对列表进行排序的地方,它返回一个列表而不是mutableList。所以newList是不可变的。将第三行替换为var newList = nums.sorted().toMutableList()将使newList变得可变,并解决您的问题。

票数 2
EN

Stack Overflow用户

发布于 2020-09-17 15:58:26

nums.sorted()的结果是一个不可变的列表,这意味着您不能更改它,换句话说,不能删除元素。

你需要告诉kotlin你想要一个可变列表var newList = nums.sorted().toMutableList()

票数 2
EN

Stack Overflow用户

发布于 2020-09-17 16:01:20

因为newList是列表对象。List object无方法removeAt

您需要使用newList.toMutableList().removeAt()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63933484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档