首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在kotlin中获取mutableList的mutableList?

如何在kotlin中获取mutableList的mutableList?
EN

Stack Overflow用户
提问于 2021-08-08 15:29:58
回答 2查看 207关注 0票数 0

我正在试着在科特林得到mutableList of LatLng pairs的mutableList。下面是我的代码。当函数onPauseButtonClicked()被调用时,我得到了locationlist变量中的值,它是mutableList of LatLng,pairs.But,我不能把这个值加到locationlists变量中,它是一个mutableListOf(locationList)。该值显示为空。代码出了什么问题。

代码语言:javascript
复制
 private var locationList = mutableListOf<LatLng>()

 private var locationlists = mutableListOf(locationList)

/**
This is function where locationlists is called
*/

 private fun onPauseButtonClicked(){

        locationlists.add(locationList)

//        Toast.makeText(requireContext() ,"$locationList", Toast.LENGTH_SHORT).show()

        val c= locationlists[0]

        Toast.makeText(requireContext() ,"$c", Toast.LENGTH_SHORT).show()

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-08 15:36:25

add会将MutableList添加到列表的末尾。但是,当您调用mutableListOf(locationList)时,您已经将一个空的MutableList作为第一个元素。在调试器中很容易验证您的位置列表将有两个元素,即使您只调用add一次。

因此,正确的方法应该是使用一个空的MutableList列表来初始化位置列表

代码语言:javascript
复制
var locationlists =  mutableListOf<MutableList<LatLng>>()
票数 1
EN

Stack Overflow用户

发布于 2021-08-08 15:36:45

在初始化使用时

代码语言:javascript
复制
private val locationLists = MutableList<MutableList<LatLang>>(0){mutableListOf()}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68702187

复制
相关文章

相似问题

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