Reputation: 654
I want to know how to make a resizeable two-dimensional array in Kotlin.
C++ example: vector< vector<int> > my_vector
What I've tried: var seqList: List<List<Int>> = ArrayList<ArrayList<Int>>()
but I'm getting an error when using seqList.add()
error: unresolved reference: add
I have read some questions regarding 2d arrays in Kotlin at stackoverflow, but they are about not-resizeable arrays or are outdated
Upvotes: 5
Views: 5198
Reputation: 89608
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))
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
.
Upvotes: 7