Can someone please tell how to create multidimensi...
# announcements
a
Can someone please tell how to create multidimensional array with some exampes
b
``Array(5) { Array(10) { Array(15) { 10 } } }``
creates array 5x10x15 with initial value 10
a
how do you print the value
d
``println(array.contentToString())``
a
the first element
b
``println(array[0][0][0])``
a
Would mind to give a example of 2D array i
3D is something bit complicated
b
2d:
``val array = Array(5) { Array(10) { 10 } }``
``println(array[0][0])``
a
I think we write
Copy code
``arrayOf<type>()``
b
it's 1d
you can write something like: arrayOf(arrayOf(1,2), arrayOf(3,4)) it will be 2d: 1,2 3,4
a
if I write
Copy code
``val array=Array(5){array(10)}``
will this mean array is empty
b
what's
``array(10)``
here? If you wanted
``arrayOf(10)``
, 10 - content, not size
a
Copy code
``val array = Array(5) { Array(10) { 10 } }``
I just didn't write
Copy code
``{10}``
b
It won't compile I believe
a
Ok
Thanks
d
That creates an array of size 5 where each element is another array of size 10. Each element in those 2nd-level arrays is also 10. So:
Copy code
``````[
[10, 10, 10, ...], // 10 times
[10, 10, 10, ...],
... // 5 times
]``````
a
So you mean each slot in array has value 10
d
The "inner" arrays, yes.
There are no two-dimensional arrays. Only arrays which contain arrays themselves (there is a difference!)
a
So how do I assign a different values
through nested loops?
d
You can use
``arrayOf(1, 2, 3)``
(creates
``[1, 2, 3]``
). Or you can use loops, or you can do something like:
``val array = Array(3) { idx -> idx * 2 }``
, which generates
``[0, 3, 6]``