-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatThatArray.java
More file actions
47 lines (40 loc) · 1.32 KB
/
FlatThatArray.java
File metadata and controls
47 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.ArrayList;
import java.util.List;
public class FlatThatArray {
public static void main(String[] args) {
Object[] unstructuredArray = new Object[] { new Object[] { 1, 2, new Integer[] { 3 } }, 4 };
List<Integer> flatenedArrayList = null;
Integer[] FlatenedArray = FlatTheArray(unstructuredArray, flatenedArrayList);
if (FlatenedArray != null) {
displayFlattenedArray(FlatenedArray);
}
}
public static Integer[] FlatTheArray(Object[] unstructuredArray, List<Integer> flatenedArrayList) {
if (flatenedArrayList == null) {
flatenedArrayList = new ArrayList<Integer>();
}
if (unstructuredArray == null) {
System.out.println("null Array at input");
return null;
}
int size = unstructuredArray.length;
for (int i = 0; i < size; i++) {
if (unstructuredArray[i] instanceof Integer) {
flatenedArrayList.add((Integer) unstructuredArray[i]);
} else {
FlatTheArray((Object[]) unstructuredArray[i], flatenedArrayList);
}
}
Integer[] flatenedArray = new Integer[flatenedArrayList.size()];
flatenedArray = flatenedArrayList.toArray(flatenedArray);
return flatenedArray;
}
/**
* @param FlatenedArray
*/
public static void displayFlattenedArray(Integer[] FlatenedArray) {
for (Integer FlattenedValue : FlatenedArray) {
System.out.print(FlattenedValue + " ");
}
}
}