fork download
  1. import java.util.*;
  2.  
  3. class flattenArray
  4. {
  5. public static void main(String args[]) {
  6. int inputLists[][] = {{1,2,24},{12,1241,122},{3,2},{7}};
  7. flattenList(inputLists);
  8. }
  9.  
  10. public static int maxLength(int array[][]) {
  11. int maxLength = 0;
  12.  
  13. for (int i = 0; i < array.length; i++) {
  14. maxLength = array[i].length > maxLength ? array[i].length : maxLength;
  15. }
  16.  
  17. return maxLength;
  18. }
  19.  
  20. public static void flattenList(int input[][]) {
  21. int maxLength = maxLength(input);
  22. List<Integer> result = new ArrayList<>();
  23.  
  24. for (int i = 0; i < maxLength; i++) {
  25. for (int j = 0; j < input.length; j++) {
  26. if (i < input[j].length) {
  27. result.add(input[j][i]);
  28. }
  29. }
  30. }
  31.  
  32. for(int i=0;i<result.size();i++){
  33. System.out.print(result.get(i)+" ");
  34. }
  35.  
  36. System.out.println();
  37. }
  38. }
Success #stdin #stdout 0.04s 320576KB
stdin
Standard input is empty
stdout
1 12 3 7 2 1241 2 24 122