```val s: Array<LongArray> = Array(5) { Long...
# getting-started
a
Copy code
val s: Array<LongArray> = Array(5) { LongArray(10) }
println("size" + s[0].size)
is this the only way to get col size ^^
k
This is an array of arrays. Each row can have a different-sized column.
s[0].size
is the number of columns in the first row. Also, note that your initializer,
Array(5) { LongArray(10) }
initializes an array of 5 rows, with the first row being a LongArray of 10 columns, and the remaining rows being null.
a
@Klitos Kyriacou so to declare some thing like java Long [] [] matrix , how can we do this in kotlin
Array(5) { LongArray(10) } [0] [0] ........ [9] [1] nul [3] nul [4] nul Did you meant to say this declaration end like this in memory ? and not [0] [0] ........ [9] [1] [0] ........ [9] [3] [0] ........ [9] [4] [0] ........ [9]
k
Sorry, you're right, it's the latter. Ignore my original reply as it's wrong.
s
Klitos' post isn't completely wrong though. not every column needs to be the same length. You could define a triangular matrix like this:
Copy code
Array(5) { LongArray(it+1) }
generates [0] [0] [1] [0][1] [2] [0] ...[2] ... [4] [0] ........ [4]
n
yes: an
Array<Array>
is not the same as a matrix because every row can have a different size. You can count the number of "cells" using `s.sumOf { it.size }`which also works (though slightly less efficient than
s.size * s[0].size
) for an array where all rows have the same length.
a
Thank you @Klitos Kyriacou @Stephan Schroeder @nkiesel for your valuable time at replying my question 🙂