There are several ways in Kotlin.
var arr = IntArray(size) // construct with only size
Then just the initial value from users or from another collection or anywhere.
var arr = IntArray(size, { 0 } ) // construct with size and fill array with 0 var arr = IntArray(size, { it * 1 } ) // construct with size and fill with its index
We can also create an array with a built-in function, for example -
var arr = intArrayOf(1, 2, 3, 4, 5) // create an array with 5 values
Another way
var arr = Array(size, { 0 } ) // it will create an integer array var arr = Array<String>(size, { "$it" } ) // this will create array with "0", "1", "2" and so on.
You can also use doubleArrayOf() or DoubleArray() or any primitive type instead of Int.
HM Nayem Jun 10 '17 at 9:23 a.m. 2017-06-10 09:23
source share