https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Ray Rahke

03/11/2024, 9:45 AM
I can use the following as a way to generate a 10x10 2d grid in a nice 1liner
Copy code
fun main() {
    val grid: List<List<Boolean>> = List(size=10) { List(size=10) { false }}
}
However this does not seem supported for mutable lists
Copy code
fun main() {
    val grid: MutableList<MutableList<Boolean>> = MutableList(size=10) { MutableList(size=10) { false }}
}
Why? And is there an analogous way to build this grid in 1 single readable expression?
c

CLOVIS

03/11/2024, 9:55 AM
What is the use-case? Why do you want a matrix that can change size? You will have no guarantees that all rows are the same length.
j

Javier

03/11/2024, 9:55 AM
r

Ray Rahke

03/11/2024, 9:57 AM
The inner grid is all that needs to be mutatable technically
it is for conway's game of life
so the inner needs to be hcangable
a cell can change from true to false
c

CLOVIS

03/11/2024, 9:58 AM
You probably should have a
List<List<Cell>>
where
Cell
is a mutable class, to avoid rows changing sizes.
Or keep
List<List<Boolean>>
and copy the matrix each time, if you want time travel.
s

Sam

03/11/2024, 9:59 AM
However this does not seem supported for mutable lists
What makes you say that? The code you posted in your question works fine for me...
c

CLOVIS

03/11/2024, 9:59 AM
But honestly, the best solution would be to use multik
s

Sam

03/11/2024, 10:02 AM
Anyway I think I'd go with
Copy code
List(10) { BooleanArray(10) }
Then you get mutable cells without being able to accidentally change the size 👍
r

Ray Rahke

03/11/2024, 10:02 AM
Oh I guess the code does work. Sworn I remmeber it not earlier
😅 1
e

ephemient

03/11/2024, 10:16 AM
a quadtree is a better representation for GoL anyway, extends to hashlife