我想知道如何在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
推荐答案科特琳有单独的 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中可调整大小的二维数组
发布评论