October 2018
Intermediate to advanced
370 pages
9h 15m
English
Kotlin allows us to declare and initialize an array with a lambda expression. The array constructor takes two parameters: the size of the array and a lambda expression. If the array is an IntArray, the expression takes an integer and returns an integer as well:
public inline constructor(size: Int, init: (Int) -> Int)
Take a look at the following example. Here, intArray has 5 elements, and each element is initialized with an incrementing value that starts from 1:
fun arrayWithLambda(){ val intArray = IntArray(5) { it } for (element in intArray){ println(element) }}
Kotlin not only declares an array of a size of 5, but it also initializes the array with a value passed to the lambda expression. The lambda ...
Read now
Unlock full access