Array declaration and initialization

In all of the examples that we have seen so far, we have declared and initialized different arrays at the same time. It is also possible, however, to declare the array first and initialize it later:

val intArray = IntArray(5)

Here, an array of integers with 5 elements is declared but is not yet initialized. When the array is declared, Kotlin initializes the array with a value of 0.

Values can be assigned in the same fashion by accessing the element's index:

intArray[0] = 1
intArray[1] = 2

Kotlin provides the set function, which is used to assign a value to an array.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233