问题描述
限时送ChatGPT账号..我想知道如何在 Kotlin 中制作可调整大小的二维数组.
C++ 示例:vector<向量
我尝试过的:var seqList: List>= ArrayList>()
但是我在使用 seqList.add() 时遇到错误
but I'm getting an error when using seqList.add()
错误:未解析的引用:添加
error: unresolved reference: add
我在 stackoverflow 上阅读了一些关于 Kotlin 中二维数组的问题,但它们是关于不可调整大小的数组或已过时
I have read some questions regarding 2d arrays in Kotlin at stackoverflow, but they are about not-resizeable arrays or are outdated
推荐答案
Kotlin 有单独的 List
和 MutableList
接口,如此处 的解释一>,例如.ArrayList
是一个 MutableList
,你只需要将它保存为一个 MutableList
变量,以便能够访问改变它的方法:>
Kotlin has separate List
and MutableList
interfaces, as explained here, for example. ArrayList
is a MutableList
, you just have to save it as a MutableList
variable in order to be able to access methods that mutate it:
val seqList: MutableList<MutableList<Int>> = ArrayList() // alternatively: = mutableListOf()
seqList.add(mutableListOf<Int>(1, 2, 3))
还要注意 mutableListOf
和 标准库中的arrayListOf
方法,这些方法可以方便地创建列表,而不是直接使用ArrayList
的构造函数.
Also note the mutableListOf
and arrayListOf
methods in the standard library, which are handy for creating lists instead of directly using the constructor of, say, ArrayList
.
这篇关于Kotlin 中可调整大小的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论