Last active
February 25, 2021 01:47
-
-
Save jjonesdesign/8bc6be025eaf246d05462fcd3f22b351 to your computer and use it in GitHub Desktop.
Flatten Multidimensional String Array of Unknown Size to Single Dimension String Array - 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
package stringarrayflattenexample | |
/** | |
* @author Jesse Jones | |
*/ | |
class StringArrayFlattenExample { | |
fun main(args:Array<String>) { | |
//Setup demo data | |
val inputArray = arrayOf<Any>(arrayOf<Any>("One", "Two", arrayOf<Any>("Three")), arrayOf<Any>("Four", arrayOf<Any>("Five", arrayOf<Any>("Six")), "Seven")) | |
//Flatten Array | |
val flatStringArray = flattenStringArray(inputArray) | |
//Print flat array to console | |
flatStringArray?.let { | |
printResults(it) | |
} | |
} | |
/** | |
* Flatten Multidimensional String Array. | |
* | |
* @param array - Array you want to flatten | |
* @return flattened String array; | |
*/ | |
fun flattenStringArray(array:Array<Any>): Array<Any>? { | |
val stringArray = ArrayList<Any>() | |
//Loop through first level of array | |
for (value in array) | |
{ | |
//Search for children within the array | |
val childResult = searchChildren(value, stringArray) | |
if (childResult != null) | |
{ | |
stringArray.add(childResult) | |
} | |
} | |
return stringArray.toArray() | |
} | |
/** | |
* Search parent for sub children | |
* | |
* @param `object` - the next item in list to search; | |
* @param list - the arraylist where results are stored; | |
* @return string of child item | |
*/ | |
private fun searchChildren(theObject:Any, list:ArrayList<Any>): String? { | |
if (theObject is String) | |
{ | |
//If child is a String and not an array, return string | |
list.add(theObject) | |
return theObject | |
} | |
else | |
{ | |
//If child is not a String, loop through child array | |
val children = theObject as Array<*> | |
for (child in children) | |
{ | |
//If not null | |
child?.let { | |
//Continue searching for children in more dimensions | |
searchChildren(it, list) | |
} | |
} | |
return null | |
} | |
} | |
/** | |
* Print the results of our flattened array to console. | |
* | |
* @param list - the flattened string array to print | |
*/ | |
private fun printResults(list: Array<Any>) { | |
for (string in list) | |
{ | |
println(string) | |
} | |
} | |
} | |
//Junit5.6 Test Class | |
import org.junit.Assert.assertArrayEquals | |
import org.junit.jupiter.api.BeforeEach | |
import org.junit.jupiter.api.DisplayName | |
import org.junit.jupiter.api.Test | |
import stringarrayflattenexample.StringArrayFlattenExample | |
/** | |
* | |
* @author Jesse Jones | |
*/ | |
class StringArrayFlattenTest { | |
lateinit var arrayFlattenExample : StringArrayFlattenExample; | |
@BeforeEach | |
fun setUp() { | |
arrayFlattenExample = StringArrayFlattenExample() | |
} | |
@Test | |
@DisplayName("Check Proper String Array Flattening") | |
fun testStringArrayFlatten() { | |
//Test input data | |
val provided = arrayOf<Any>(arrayOf<Any>("One", "Two", arrayOf<Any>("Three")), arrayOf<Any>("Four", arrayOf<Any>("Five", arrayOf<Any>("Six")), "Seven")) | |
//Expected test result data | |
val expected = arrayOf<Any>("One", "Two", "Three", "Four", "Five", "Six", "Seven") | |
assertArrayEquals(expected, arrayFlattenExample.flattenStringArray(provided)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment