https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Sudhir Singh Khanger

12/29/2020, 5:35 AM
One would use
intArrayOf()
if one wants to initialize an
IntArray
with specific and different values otherwise one can simply use
IntArray()
. Is that correct?
Copy code
* These two are the same

val arr: IntArray = intArrayOf(0, 0, 0)
val arr = IntArray(3)

* IntArray with different values

val arr = IntArray(3)
arr[0] = 1
arr[0] = 2
arr[0] = 3

vs

val arr: IntArray = intArrayOf(1, 2, 3)
e

ephemient

12/29/2020, 5:41 AM
also
Copy code
IntArray(3) { it + 1 } // same result as intArrayOf(1, 2, 3)
useful to initialize an array with a constant or pattern
2 Views