I cannot wrap my head around the following, say I have a List, each list contains a ‘would be’ for loop. each succession should be within one another.
So if I have a list with 3 objects, I want
class Focus {
String focus;
List<String> values;
public Focus(String focus, String... values) {
this.focus = focus;
this.values = Lists.newArrayList(values);
}
}
List<Focus> focuses = new ArrayList<Focus>();
focuses.add(new Focus("Focus 1", "09", "14", "13", "12"));
focuses.add(new Focus("Focus 2", "94", "92"));
focuses.add(new Focus("Focus 3", "A", "B"));
String my_string = "";
for (Focus obj1 : list_obj_x) {
for (Focus obj2 : list_obj_xx) {
for (Focus obj3 : list_obj_xxx) {
my_string += obj1 + " " + obj2 + " " + obj3;
}
}
}
obviously with a list the for-loop structure can grow, and the above is not possible.
i need a dynamic structure to cater for the my_string need. i.e:
94 09 A
94 14 A
94 13 A
94 12 A
94 09 B
94 14 B
94 13 B
94 12 B
92 09 A
92 14 A
92 13 A
92 12 A
92 09 B
92 14 B
92 13 B
92 12 B
the output should be like the above.
this is what I have so far:
int focusCount = focuses.size();
for (int i = (focusCount - 1); i >= 0; i--) {
Focus currentFocus = focuses.get(i);
List<String> currentFocusValues = currentFocus.values;
for (int cfv = 0; cfv < currentFocusValues.size(); cfv++) {
String currentFocusValue = currentFocusValues.get(cfv);
for (int j = (i - 1); j >= 0; j--) {
Focus previousFocus = focuses.get(j);
List<String> previousFocusValues = previousFocus.values;
for (int pfv = 0; pfv < previousFocusValues.size(); pfv++) {
String previousFocusValue = previousFocusValues.get(pfv);
System.out.println(currentFocusValue + " " + previousFocusValue);
}
}
}
}
it caters for all combinations of the list values,
but not in the structure I want.
Can someone please help me?
The most straightforward approach would probably be recursion. In each step of the recursion, you “pin down” the value of the n-th list one by one, then recurse down the “list of lists” until you reach the end.
With the recursive method
Of course, this can be refined further, but maybe it suffices as a starting point for you.