January 2018
Intermediate to advanced
434 pages
14h 1m
English
You can also create your own function by extending Kotlin's code. For example, creating a method, as follows:
inline fun <reified inside> array2d(sizeOuter: Int, sizeInner: Int, noinline innerInit: (Int)->inside): Array<Array<inside>> = Array(sizeOuter) { Array<inside>(sizeInner, innerInit) }
This can enable you to create a 2D array quite easily, just by doing the following:
array2d(10,10,{0})
You can also create a list of lists in a similar fashion. Here's an example of a list of lists:
fun main(args: Array<String>) { val a= listOf(listOf(1,2,3), listOf(4,5,6), listOf(7,8,9)) a.forEach { print(" ${it} ") }}
This is its output:
[1, 2, 3] [4, 5, 6] [7, 8, 9]
Read now
Unlock full access