Kotlin 中可调整大小的二维数组 [英] Resizeable two-dimensional array in Kotlin

查看:28
本文介绍了Kotlin 中可调整大小的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 Kotlin 中制作可调整大小的二维数组.

C++ 示例:vector<向量>my_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 有单独的 ListMutableList 接口,如此处 的解释一>,例如.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆