-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFlattenMatrix.java
42 lines (31 loc) · 983 Bytes
/
FlattenMatrix.java
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
public class flattenArray {
public static void main(String args[]) {
int inputLists[][] = {{1,2,24},{12,1241,122},{3,2},{7}};
flattenList(inputLists);
}
public static void flattenList(int input[][]){
List result = new ArrayList();
Map<Integer, List<Integer>> elements = new HashMap<Integer,List<Integer>>();
for(int i=0;i<input.length;i++){
for(int j=0;j<input[i].length;j++){
List<Integer> temp = new ArrayList<Integer>();
if(elements.get(j)!=null) {
temp=elements.get(j);
temp.add(input[i][j]);
elements.put(j, temp);
}
else {
temp.add(input[i][j]);
elements.put(j,temp);
}
}
}
for(int i=0;i<input.length;i++){
if(elements.get(i)!=null)
result.addAll(elements.get(i));
}
for(int i=0;i<result.size();i++){
System.out.println(result.get(i)+" ");
}
}
}