Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- public class ArrayFlattener {
- private static ArrayList<Integer> aList = new ArrayList<Integer>();
- public ArrayFlattener(){
- aList = new ArrayList<Integer>();
- }
- public static Integer[] Expend(Object[] arr){
- int length = arr.length;
- Object[] mainArr = arr;
- for(int i =0 ; i < length; i++){
- /*
- * check if the first element of main array is an array,
- * if the element is not an array, we add the int to our ArrayList
- * if the first element is an array, we perform a recursive call on that array
- */
- Object a = mainArr[i];
- if(a.getClass().isArray()){
- Expend((Object[]) a);
- }
- else{
- aList.add( (Integer) mainArr[i]);
- }
- }
- // convert the ArrayList back to an array
- return aList.toArray(new Integer[aList.size()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement