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
althaf
08/26/2021, 8:28 AM
@Klitos Kyriacou so to declare some thing like java
Long [] [] matrix , how can we do this in kotlin
althaf
08/26/2021, 8:32 AM
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
Klitos Kyriacou
08/26/2021, 9:17 AM
Sorry, you're right, it's the latter. Ignore my original reply as it's wrong.
s
Stephan Schroeder
08/26/2021, 12:09 PM
Klitos' post isn't completely wrong though. not every column needs to be the same length. You could define a triangular matrix like this:
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
althaf
08/27/2021, 3:07 AM
Thank you @Klitos Kyriacou@Stephan Schroeder@nkiesel for your valuable time at replying my question 🙂