Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3.  
  4.  
  5. public class ArrayFlattener {
  6.  
  7. private static ArrayList<Integer> aList = new ArrayList<Integer>();
  8.  
  9. public ArrayFlattener(){
  10. aList = new ArrayList<Integer>();
  11. }
  12.  
  13. public static Integer[] Expend(Object[] arr){
  14.  
  15. int length = arr.length;
  16. Object[] mainArr = arr;
  17.  
  18. for(int i =0 ; i < length; i++){
  19.  
  20. /*
  21. * check if the first element of main array is an array,
  22. * if the element is not an array, we add the int to our ArrayList
  23. * if the first element is an array, we perform a recursive call on that array
  24. */
  25.  
  26. Object a = mainArr[i];
  27. if(a.getClass().isArray()){
  28. Expend((Object[]) a);
  29. }
  30. else{
  31. aList.add( (Integer) mainArr[i]);
  32. }
  33.  
  34. }
  35.  
  36. // convert the ArrayList back to an array
  37. return aList.toArray(new Integer[aList.size()]);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement