Last active
November 21, 2018 11:07
-
-
Save edpichler/68c176b9de669366bccdb5baab2aaa29 to your computer and use it in GitHub Desktop.
Flat an Int Array in Kotlin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun flatten(numbers: Array<Any>): ArrayList<Int> { | |
val flattenReturn = arrayListOf<Int>() | |
numbers.forEach { it -> | |
if (it is Int) { | |
flattenReturn.add(it) | |
} else { | |
val temp = flatten(it as Array<Any>) | |
flattenReturn.addAll(temp) | |
} | |
} | |
return flattenReturn; | |
} | |
/** | |
* Test the method. | |
*/ | |
fun main(args: Array<String>) { | |
val first: Array<Any> = arrayOf(1, 2, arrayOf(3, 4)) | |
val flatThis : Array<Any> = arrayOf(first, 5, 6) | |
val flatten = flatten(flatThis) | |
println(flatten) // this prints [1, 2, 3, 4, 5, 6] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment