Kotlin - Array Initialization Example

Kotlin arrays are created with functions such as arrayOf() or intArrayOf(), or with classes such as IntArray or FloatArray.

Kotlin Array Initialization Example

In the first example, we show how we can initialize arrays in Kotlin.
package net.javaguides.kotlin.examples

import java.util.Arrays

fun main(args: Array < String > ) {
    val nums = arrayOf(1, 2, 3, 4, 5)
    println(Arrays.toString(nums))

    val nums2 = (3. .12).toList().toTypedArray()
    println(Arrays.toString(nums2))

    val nums3 = IntArray(5, {
        i - > i * 2 + 3
    })
    println(Arrays.toString(nums3))
}
Output:
[1, 2, 3, 4, 5]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[3, 5, 7, 9, 11]
An array is created with arrayOf() function:
  val nums = arrayOf(1, 2, 3, 4, 5)
We created an array from a range of numbers:

 val nums2 = (3. .12).toList().toTypedArray()




Comments