I want to build a "2D array", and set and change e...
# announcements
m
I want to build a "2D array", and set and change elements in it. I think a mutableList does not work well, because i cant not randomly set elements in it. It gives a index out of bound exception. Is there a autogrow option? or should i use a different data structure?
o
You want to do myList.set(34,"example") and if index 34 doesn't exist, you want to grow list to this size (or larger)? I don' think there is a list for this, but you could always make your own to do exactly what you want or make a function that check size and add null or empty value until your index
n
wild guess: AoC day 11. Try
Array<CharArray>
or
Array<IntArray>
m
the array works for me 🙂
@Ole K. Øverland (Norway) you mean i need ti implmenet the interface myself? since i can not grow a mutable list myself, right?
n
fun toArray(l: List<String>) = l.map { it.toCharArray() }.toTypedArray()
. and then use
data[row][col]
to access (and set) chars in that 2D
alternative is to use just a
CharArray
but that needs more juggling with with
%
to compute the correct index
m
solved the advent of code task 😄