In Java an array can be initialized such as:
int numbers[] = new int[] {10, 20, 30, 40, 50}
How does Kotlin's array initialization look like?
In Kotlin There are Several Ways. Then simply initial value from users or from another collection or wherever you want. 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.
val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50)
See Kotlin - Basic Types for details.
You can also provide an initializer function as a second parameter:
val numbers = IntArray(5) { 10 * (it + 1) } // [10, 20, 30, 40, 50]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With