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

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

问题描述

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

C ++示例:vector< vector<int> > my_vector

我尝试过的操作:var seqList: List<List<Int>> = ArrayList<ArrayList<Int>>()

但是使用seqList.add()时出现错误

but I'm getting an error when using seqList.add()

错误:未解决的参考:添加

error: unresolved reference: add

我已经在stackoverflow上阅读了有关Kotlin中2d数组的一些问题,但它们与不可调整大小的数组有关或已过时

I have read some questions regarding 2d arrays in Kotlin at stackoverflow, but they are about not-resizeable arrays or are outdated

推荐答案

科特琳有单独的 MutableList 接口,例如,如此处所述. ArrayListMutableList,您只需将其另存为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天全站免登陆