Created
May 14, 2016 07:51
-
-
Save antoniomartel/54b1139cb70580c7e36b44f122f0a065 to your computer and use it in GitHub Desktop.
Class to flatten nested array of Integers
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 com.antoniomartel; | |
import java.util.ArrayList; | |
public class FlatArray { | |
protected ArrayList nestedArray = null; | |
protected ArrayList flattenArray = null; | |
// New exception created to be thrown when nested array contains something different from Integers | |
class NotAnArrayOfIntegersException extends Exception { | |
private static final long serialVersionUID = 1L; | |
public NotAnArrayOfIntegersException (String msg){ | |
super(msg); | |
} | |
} | |
// Class constructor | |
public FlatArray(ArrayList arrayToBeFlatten) { | |
nestedArray = arrayToBeFlatten; | |
} | |
// Private method to recursively loop thru arrays while adding found Integers to the flatten array that will come out as a result | |
private void loopArray(ArrayList array) throws NotAnArrayOfIntegersException { | |
if (array != null) { | |
for (int i = 0; i < array.size(); i++) { | |
if (array.get(i).getClass().getName() == "java.lang.Integer") { | |
flattenArray.add(array.get(i)); | |
} | |
else | |
if (array.get(i).getClass().getName() == "java.util.ArrayList") { | |
ArrayList subArray = (ArrayList) array.get(i); | |
loopArray(subArray); | |
} | |
else { | |
throw new NotAnArrayOfIntegersException("Nested array contains a class different from array of Integers: " + array.get(i).getClass().getName()); | |
} | |
} | |
} | |
} | |
// Public method that flattens arrays | |
public ArrayList toArray() throws NotAnArrayOfIntegersException { | |
flattenArray = new ArrayList<Integer>(); | |
loopArray(nestedArray); | |
return flattenArray; | |
} | |
// Main method to create test env, execute method, print and check for result | |
public static void main(String[] args) throws NotAnArrayOfIntegersException { | |
ArrayList<Integer> array1 = new ArrayList<Integer>(); | |
array1.add(3); | |
ArrayList array2 = new ArrayList(); | |
array2.add(1); | |
array2.add(2); | |
array2.add(array1); | |
ArrayList array3 = new ArrayList(); | |
array3.add(array2); | |
array3.add(4); | |
FlatArray nestedArray = new FlatArray(array3); | |
String result = nestedArray.toArray().toString(); | |
System.out.printf("Array to be flatten is : %s\n", array3.toString()); | |
System.out.printf("Flatten array is : %s\n", result); | |
if (result.equals("[1, 2, 3, 4]") == false) | |
System.out.println("There was an error when nested array was tried to flatten"); | |
else | |
System.out.println("Got expected result"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Class FlatArray that transforms a nested arrays of Integers to a flatten array of Integers (no subarrays inside)