Last active
February 25, 2021 01:44
-
-
Save jjonesdesign/98f2a066572610c5ab44b9f5ee05dbf5 to your computer and use it in GitHub Desktop.
Flatten Multidimensional String Array of Unknown Size to Single Dimension String Array - Java
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; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* @author Jesse Jones | |
*/ | |
public class StringArrayFlattenExample { | |
public static void main(String[] args) { | |
//Setup demo data | |
Object[] inputArray = new Object[]{ | |
new Object[]{"One", "Two", new Object[]{"Three"}}, | |
new Object[]{"Four", new Object[]{"Five", new Object[]{"Six"}}, "Seven"} | |
}; | |
//Flatten Array | |
Object[] flatStringArray = flattenStringArray(inputArray); | |
//Print flat array to console | |
printResults(flatStringArray); | |
} | |
/** | |
* Flatten Multidimensional String Array. | |
* | |
* @param array - Array you want to flatten | |
* @return flattened String array; | |
*/ | |
public static Object[] flattenStringArray(Object[] array) { | |
List<String> stringArray = new ArrayList<String>(); | |
if (array == null) { | |
System.out.println("Array Was Null"); | |
return null; | |
} | |
//Loop through first level of array | |
for (Object value : array) { | |
//Search for children within the array | |
Object childResult = searchChildren(value, stringArray); | |
if (childResult != null) { | |
stringArray.add((String) childResult); | |
} | |
} | |
return stringArray.toArray(); | |
} | |
/** | |
* Search parent for sub children | |
* | |
* @param object - the next item in list to search; | |
* @param list - the list where results are stored; | |
* @return string of child item | |
*/ | |
private static String searchChildren(Object object, List list) { | |
if (object instanceof String) { | |
//If child is a String and not an array, return string | |
list.add((String) object); | |
return (String) object; | |
} else { | |
//If child is not a String, loop through child array | |
Object[] children = (Object[]) object; | |
for (Object child : children) { | |
//Continue searching for children in more dimensions | |
searchChildren(child, list); | |
} | |
return null; | |
} | |
} | |
/** | |
* Print the results of our flattened array to console. | |
* | |
* @param list - the flattened string array to print | |
*/ | |
public static void printResults(Object[] list) { | |
if (list != null) { | |
for (Object string : list) { | |
System.out.println(string); | |
} | |
} | |
} | |
} | |
//Junit5.6 Test Class | |
import static org.junit.jupiter.api.Assertions.*; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.DisplayName; | |
import org.junit.jupiter.api.Test; | |
import stringarrayflattenexample.StringArrayFlattenExample; | |
/** | |
* | |
* @author Jesse Jones | |
*/ | |
public class StringArrayFlattenTest { | |
private StringArrayFlattenExample arrayFlattenExample; | |
@BeforeEach | |
public void setUp(){ | |
arrayFlattenExample = new StringArrayFlattenExample(); | |
} | |
@Test | |
@DisplayName("Check Proper String Array Flattening") | |
public void testStringArrayFlatten(){ | |
//Test input data | |
Object[] provided = new Object[]{ | |
new Object[]{"One", "Two", new Object[]{"Three"}}, | |
new Object[]{"Four", new Object[]{"Five", new Object[]{"Six"}}, "Seven"} | |
}; | |
//Expected test result data | |
Object[] expected = {"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