Kotlin中可调整大小的二维数组

编程入门 行业动态 更新时间:2024-10-07 04:31:50
本文介绍了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

推荐答案

科特琳有单独的 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

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中可调整大小的二维数组

本文发布于:2023-11-29 15:40:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   可调整   大小   Kotlin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!