返回组合列表

时间:2013-12-04 11:07:01

标签: java combinations

我已经完成了这些功能,但我目前仍然坚持实施。当它涉及多个变量时,特别难以考虑它。

我想要的内容:输入[["A", "B"], ["0", "1", "2"]],我想要输出[["A", "0"], ["A", "1"], ["A", "2"], ["B", "0"], ["B", "1"], ["B", "2"]]。我希望这很清楚。

我的代码到现在为止:

/**
 * Returns a list of all possible combinations of the entered lists.
 *
 * Example: [["A", "B"], ["0", "1", "2"]]
 * Returns: [["A", "0"], ["A", "1"], ["A", "2"], ["B", "0"], ["B", "1"], ["B", "2"]]
 *
 * @param <T> The type parameter
 * @param elements An array of lists
 * @return All possible combinations of the entered lists
 */
public static <T> List<List<T>> createCombinations(List<T>... elements) {
    int[] i = new int[elements.length];
    List<List<T>> returnLists = new ArrayList<>();

    for (int j = 0; j < elements.length; j++) {

    }

    return returnLists;
}

public static <T> List<List<T>> createCombinations(List<List<T>> elements) {
    return createCombinations((List<T>[])elements.toArray());
}

编辑:关于我实际想要如何使用它可能有点混乱,但这是如何:
List<List<String>> combinations = Utils.createCombinations(cocNumbers, vatNumbers, ibans);

3 个答案:

答案 0 :(得分:2)

一种简单的方法是递归方法。让方法为第一个列表执行for循环以遍历其所有值。每次选择一个,它会在删除第一个列表时自行调用。然后它将它选择的值添加到递归调用的返回值。返回由for循环创建的所有这些值的列表。

伪代码:

public static <T> List<List<T>> createCombinations(List<T>... elements) {
    (call non-vararg function)
}

public static <T> List<List<T>> createCombinations(List<List<T>> elements) {
    if (elements is empty) {
        return empty list
    } 
    List<T> head = get first list in elements
    List<T> tail = everything in elements except the first list
    List<T> allCombinations = empty list
    List<T> subcombinations = createCombinations(tail);
    for(every value in head) {
        List<T> x = copy of subcombinations with current value added to front
        add x to allCombinations
    }
    retyrn allCombinations;
}

答案 1 :(得分:0)

在for循环中使用for循环。外部循环遍历每个组合中第一个元素的数组,内部循环遍历另一个数组。

答案 2 :(得分:0)

最终弄清楚自己。

代码本身:

/**
 * Returns a list of all possible combinations of the entered array of lists.
 *
 * Example: [["A", "B"], ["0", "1", "2"]]
 * Returns: [["A", "0"], ["A", "1"], ["A", "2"], ["B", "0"], ["B", "1"], ["B", "2"]]
 *
 * @param <T> The type parameter
 * @param elements An array of lists
 * @return All possible combinations of the entered lists
 */
public static <T> List<List<T>> createCombinations(List<T>... elements) {
    List<List<T>> returnLists = new ArrayList<>();

    int[] indices = new int[elements.length];
    for (int i = 0; i < indices.length; i++) {
        indices[i] = 0;
    }

    returnLists.add(generateCombination(indices, elements));
    while (returnLists.size() < countCombinations(elements)) {
        gotoNextIndex(indices, elements);
        returnLists.add(generateCombination(indices, elements));
    }

    return returnLists;
}

/**
 * Jumps to the next index for creating combinations.
 *
 * This method updates the indices.
 *
 * @param <T> The type parameter
 * @param indices The index positions
 * @param elements Array of lists
 */
private static <T> void gotoNextIndex(int[] indices, List<T>... elements) {
    for (int j = indices.length - 1; j >= 0; j--) {
        if (indices[j] == elements[j].size() - 1) {
            indices[j] = 0;
            continue;
        }
        indices[j]++;
        return;
    }
}

/**
 * Return a combination in the form of a list based on the current indices.
 *
 * @param <T> The type parameter
 * @param indices The index positions
 * @param elements Array of lists
 * @return The combination
 */
private static <T> List<T> generateCombination(int[] indices, List<T>... elements) {
    List<T> returnList = new ArrayList<>();
    for (int i = 0; i < indices.length; i++) {
        returnList.add(elements[i].get(indices[i]));
    }
    return returnList;
}

/**
 * Counts the possible number of combinations in this array of lists.
 *
 * @param <T> The type parameter
 * @param elements Array of lists
 * @return Possible number of combinations
 */
private static <T> int countCombinations(List<T>... elements) {
    int count = 1;
    for (List<T> list : elements) {
        count *= list.size();
    }
    return count;
}

/**
 * Returns a list of all possible combinations of the entered list of lists.
 *
 * Example: [["A", "B"], ["0", "1", "2"]]
 * Returns: [["A", "0"], ["A", "1"], ["A", "2"], ["B", "0"], ["B", "1"], ["B", "2"]]
 *
 * @param <T> The type parameter
 * @param elements A list of lists
 * @return All possible combinations of the entered lists
 */
public static <T> List<List<T>> createCombinations(List<List<T>> elements) {
    return createCombinations((List<T>[])elements.toArray());
}

示例:

List<String> l1 = new ArrayList<>();
l1.add("A");
l1.add("B");
List<String> l2 = new ArrayList<>();
l2.add("0");
l2.add("1");
l2.add("2");
List<String> l3 = new ArrayList<>();
l3.add("w");
l3.add("x");
l3.add("y");
l3.add("z");
List<List<String>> combs = Utils.createCombinations(l1, l2, l3);
for (List<String> l : combs) {
    System.out.println("l = " + l);
}

示例输出:

l = [A, 0, w]
l = [A, 0, x]
l = [A, 0, y]
l = [A, 0, z]
l = [A, 1, w]
l = [A, 1, x]
l = [A, 1, y]
l = [A, 1, z]
l = [A, 2, w]
l = [A, 2, x]
l = [A, 2, y]
l = [A, 2, z]
l = [B, 0, w]
l = [B, 0, x]
l = [B, 0, y]
l = [B, 0, z]
l = [B, 1, w]
l = [B, 1, x]
l = [B, 1, y]
l = [B, 1, z]
l = [B, 2, w]
l = [B, 2, x]
l = [B, 2, y]
l = [B, 2, z]