动态“for-loop”结构

时间:2012-02-03 13:05:47

标签: java algorithm data-structures

我无法绕过以下内容,说我有一个List,每个列表都包含一个'will be'for循环。每个继承应该在彼此之内。

所以,如果我有一个包含3个对象的列表,我想要

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;
        }
    }
}

显然有一个列表,for循环结构可以增长,而上述是不可能的。 我需要一个动态结构来满足my_string的需求。即:

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 

输出应该如上所述。 这就是我到目前为止所做的:

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);
            }
        }
    }
}

它适合列表值的所有组合, 但不是我想要的结构。

有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:3)

最简单的方法可能是递归。在递归的每个步骤中,您逐个“确定”第n个列表的值,然后递归“列表列表”直到结束。

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

使用递归方法

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()...
        }
    }
}

当然,这可以进一步完善,但也许它足以作为你的起点。

答案 1 :(得分:1)

这是一种迭代方法(仍需要清理):

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;
    }
}

答案 2 :(得分:0)

你只有2个循环:1个用于Focus-objects,1个用于它们的值。

答案 3 :(得分:0)

正如Kurt已经提到的,你只需要两个循环。

您有一个对象列表,Focus对象。每个对象都有一个值列表。因此,您需要一个外部循环来遍历对象列表,另一个循环遍历正在处理的对象的值列表:

mystring = ""
int m = get the length of focuses list
for (1 through m) {
 int n = geht the length of values list for the current Focus object
 for (1 through n) {
    myString += current value;
 }

}
相关问题