https://kotlinlang.org logo
Title
m

mending3

11/23/2019, 3:59 PM
how do I write two dimensional array in kotlin? in php we can write it very easily, such as
$arr = [
    [
        "name" => "john"
    ],
    [
        "name" => "michael"
    ]
];
how do I achieve the above?
m

Mark Murphy

11/23/2019, 4:15 PM
arrayOf(arrayOf(...), arrayOf(...))
, where
...
is what you want in the inner arrays
s

Shawn

11/23/2019, 4:16 PM
You’ll need mapOf for associative arrays
m

mending3

11/23/2019, 4:27 PM
excuse me, gosh... really bother tho. wish the kotlin team adopt the square bracket notation just like php does
s

Shawn

11/23/2019, 4:35 PM
listOf(
  mapOf(“name” to “John”),
  mapOf(“name” to “michael”)
)
But like, don’t do that. Don’t write PHP in Kotlin. You probably want to define a Person data class or something similar here
☝️ 7
m

mending3

11/23/2019, 4:45 PM
how do I define the data class in this regard?
would you show me the complete code of this?
m

Mark Murphy

11/23/2019, 4:46 PM
data class Person(val name: String)

listOf(Person("John"), Person("michael"))
m

mending3

11/23/2019, 4:49 PM
@Mark Murphy finally, the light is shed
💡 1
thank you
s

Shawn

11/23/2019, 4:54 PM
You should probably have a look at the docs. https://kotlinlang.org/docs/reference/data-classes.html
I would also recommend going through the Koans https://kotlinlang.org/docs/tutorials/koans.html
m

Matteo Mirk

11/25/2019, 12:44 PM
just for fun, trying to bend the language you could get close to square brackets notation: https://pl.kotl.in/yzsMKPkcB
👀 1