How to get multi-dimenstional primitive array class in kotlin? How to get multi-dimenstional primitive array class in kotlin? json json

How to get multi-dimenstional primitive array class in kotlin?


Answer to the Gson problem

For the class type of your use case use Array<DoubleArray>::class.java)

Some additional Words on Multidimensional Arrays

Simply wrap arrayOf into another arrayOf or doubleArrayOf (less Boxing overhead) to get something like Array<DoubleArray>:

val doubles : Array<DoubleArray>  = arrayOf(doubleArrayOf(1.2), doubleArrayOf(2.3))

It's also possible to nest multiple Array initializers with the following constructor:

public inline constructor(size: Int, init: (Int) -> T)

A call can look like this:

val doubles2: Array<DoubleArray> = Array(2) { i ->    DoubleArray(2) { j ->        j + 1 * (i + 1).toDouble()    }}//[[1.0, 2.0], [2.0, 3.0]]


In the future, you can try using the Kotlin converter. I took your code and ran it through the converter and got the following working code which agrees with the answer given.

internal var json = "[[1.2, 4.1], [3.4, 4.4]]"internal var variable = Gson().fromJson(json, Array<DoubleArray>::class.java)


You can mix arrayOf and doubleArrayOf for that case.

arrayOf(    doubleArrayOf(1.2, 4.1)    doubleArrayOf(3.4, 4.4))