January 2018
Intermediate to advanced
434 pages
14h 1m
English
We will be using the .flatten method of the kotlin.stdlib library. It takes in an array or collection and returns a single list of all elements from all collections/arrays in the given collection/array.
For example, with an array of arrays:
[[1,2,3],[1,2,3],[1,2,3]] -> [1,2,3,1,2,3,1,2,3]
fun main(args: Array<String>) { val a= arrayOf(arrayOf(1,2,3),arrayOf(1,2,3),arrayOf(1,2,3)) a.flatten().forEach { print(" ${it} ") }}//Output: 1 2 3 1 2 3 1 2 3
For example, with a list of lists:
[[1,2,3],[1,2,3],[1,2,3]] -> [1,2,3,1,2,3,1,2,3]
fun main(args: Array<String>) { val a= listOf(listOf(1,2,3),listOf(1,2,3),listOf(1,2,3)) a.flatten().forEach { print(" ${it} ") }}
Read now
Unlock full access