Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic 'for-loop' Structure

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?

like image 299
epoch Avatar asked Oct 28 '25 12:10

epoch


2 Answers

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.

String[] values = new String[focuses.size()];
CreateCombinations(focuses, 0, values);

With the recursive method

private void CreateCombinations(List<Focus> focuses, int index, string[] values) {
    Focus focus = focuses.get(index);
    for (string v : focus.values) {
        values[index] = v;
        if (index < focuses.size() - 1) {
            // there is at least one other focus
            CreateCombinations(focuses, index+1, values);
        } else {
            // all values pinned down
            StringBuilder sb = new StringBuilder(values[0]);
            for (int i = 1; i < values.length; ++i) {
                sb.append(" ").append(values[i]);
            }
            // now do whatever you like to do with sb.toString()...
        }
    }
}

Of course, this can be refined further, but maybe it suffices as a starting point for you.

like image 178
Andreas Baus Avatar answered Oct 30 '25 01:10

Andreas Baus


Here's an iterative approach (needs cleaned up still):

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Focus {
    String focus;
    List<String> values;

    public Focus(String focus, String... values) {
        this.focus = focus;
        this.values = Arrays.asList(values);
    }

    public static String printAllCombinations(Focus... focuses) {
        String myString = "";
        List<String> allCombinations = new ArrayList<String>();

        int length = focuses.length;

        if (length == 0) {
            return "";
        } else if (length == 1) {
            allCombinations = focuses[0].values;
        } else if (length > 1) {
            for (Focus f : focuses) {
                allCombinations = getCombinations(allCombinations, f.values);
            }
        } 

        for (String s : allCombinations) {
            myString += s+"\n";
        }

        return myString;
    }

    private static List<String> getCombinations(List<String> l1, List<String> l2) {
        if (l1.size() == 0) {return l2;}
        else if (l2.size() == 0) {return l1;}

        List<String> combinations = new ArrayList<String>();
        for (String outerValue : l1) {
            for (String innerValue : l2) {
                combinations.add(outerValue + " " + innerValue);
            }
        }
        return combinations;
    }
}
like image 38
csturtz Avatar answered Oct 30 '25 01:10

csturtz