I have a list of integer lists List<List<Integer>> dependency = new ArrayList<List<Integer>>()
that contains the following lists:
[0, 0]
[1, 0]
[2, 1]
[3, 1]
[4, 3]
[5, 1]
[6, 4]
[8, 5]
[9, 3]
[10, 2]
[11, 6]
Each list consists of ID and its dependent from another list. For example, [2,1]
can be read as the ID of 2 depends on the list of ID = 1 ([1,0]
).
I would like to know the dependencies between the lists similar to the following example:
10 <- 2 <- 1 <- 0 (this can be read as: List of ID = 10 depends on list of ID = 2 that depends on list of ID = 1 that depends on list of ID = 0)
8 <- 2 <- 1 <- 0 (List of ID = 8 depends on list of ID = 5 that depends on list of ID = 1 that depends on list of ID = 0)
To do that, I did the following:
for(List<Integer> x: dependency) {
x1 = x.get(0);
x2 = x.get(1);
int x3 = dependency.get(x2).get(1);
System.out.println(x1 + " -- " + x2 + " -- " + x3);
}
But this doesn't work with the case of ID = 6 since it has more dependencies: 6 <- 4 <- 3 <- 1 <- 0
Also, in the case of 11: 11 <- 6 <- 4 <- 3 <- 1 <- 0
How to fix the issue so that I can get as many dependencies as possible?