-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab7q3.java
More file actions
41 lines (34 loc) · 1.11 KB
/
Lab7q3.java
File metadata and controls
41 lines (34 loc) · 1.11 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
import java.util.ArrayList;
public class Lab7q3 {
public static void main(String[] args) {
int [][] matrix = {
{0,0,0,0},
{1,0,1,0},
{1,0,0,1},
{0,1,0,0}
};
ArrayList<ArrayList<Integer>> list = convertMatrixToList(matrix);
for(int i=0; i<list.size(); i++) {
System.out.print(i + ": ");
for(int j : list.get(i)) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static ArrayList<ArrayList<Integer>> convertMatrixToList(int[][] matrix) {
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
int vertices = matrix.length;
for(int i=0; i<vertices; i++) {
list.add(new ArrayList<Integer>());
}
for(int i=0; i<vertices; i++) {
for(int j=0; j<vertices; j++) {
if(matrix[i][j] == 1 && !list.get(i).contains(j)) {
list.get(i).add(j);
}
}
}
return list;
}
}