从不确定的数量中查找所有可能的组合

问题描述 投票:1回答:2

给出列表; List<List<SomeType>>内部列表可以是任何大小,外部列表是传递给函数的变量的大小,我需要找到包含元素的所有可能组合。

因为我的解释可能很差,所以这里是示例输入和输出。

输入:

[
    ['a', 'b'],
    ['c', 'd', 'e', 'f'],
    ['g', 'h'],
    ['i']
]

输出:

[
    ['a', 'c', 'g', 'i'],
    ['a', 'c', 'h', 'i'],
    ['a', 'd', 'g', 'i'],
    ['a', 'd', 'h', 'i'],
    ['a', 'e', 'g', 'i'],
    ['a', 'e', 'h', 'i'],
    ['a', 'f', 'g', 'i'],
    ['a', 'f', 'h', 'i'],
    ['b', 'c', 'g', 'i'],
    ['b', 'c', 'h', 'i'],
    ['b', 'd', 'g', 'i'],
    ['b', 'd', 'h', 'i'],
    ['b', 'e', 'g', 'i'],
    ['b', 'e', 'h', 'i'],
    ['b', 'f', 'g', 'i'],
    ['b', 'f', 'h', 'i']
]

我对如何完成此任务感到非常困惑。我根本无法解决问题,任何帮助将不胜感激。

java arrays list arraylist multidimensional-array
2个回答
0
投票

检查一下,我认为这可以解决您的问题

    public class Test {

        public static void main(String a[]) {
            List<List<String>> lists = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d", "e", "f"), Arrays.asList("g", "h"), Arrays.asList("i"));
            System.out.println("lists = " + lists);
            List<List<String>> out = getOutPut(new ArrayList<>(), lists);
            System.out.println("out = " + out);
        }

        private static List<List<String>> getOutPut(List<String> conList, List<List<String>> lists) {
            List<List<String>> out = new ArrayList<>();
            if (lists.size() == 1) {
                lists.get(0).forEach(s -> {
                    List<String> newList = new ArrayList<>(conList);
                    newList.add(s);
                    out.add(newList);
                });
            } else {
                lists.get(0).forEach(s -> {
                    List<String> newList = new ArrayList<>(conList);
                    newList.add(s);
                    out.addAll(getOutPut(newList, lists.subList(1, lists.size())));
                });
            }
            return out;
        }
    }

0
投票

这是我的实现。似乎工作正常。我使用String [] []作为输入,但是您可以根据需要进行更改。

public class Main {

    /**
     * This method nests (input.length) for loops to build the desired output.
     * @param input The input, change the data type as needed
     * @param i An index used to keep track of the element we are checking within the nested for loops
     * @param output The output, again, change the data type as needed
     * @param newElementOutput A String[] used to temporarily hold the elements of each String[] in the output.
     */
    private static void runThrough(String[][] input, int i, ArrayList<String[]> output, String[] newElementOutput){

        if(i < input.length - 1){
            for(String element:input[i]){
                newElementOutput[i] = element;
                runThrough(input, i+1, output, newElementOutput);
            }
        }else if(i == input.length - 1){
            for(String element:input[i]){
                newElementOutput[i] = element;
                output.add(Arrays.copyOf(newElementOutput, input.length));  // a copy so when newOutput changes, the array added to output will stay the same since the reference is different
            }
        }
    }

    public static void getCombinations(String[][] input, boolean printTest){
        String[] newOutput = new String[input.length];
        ArrayList<String[]> output = new ArrayList<>();
        runThrough(input, 0, output, newOutput);

        // Print the results, to check that everything is as it should
        if (printTest) printResults(output);
    }

    private static void printResults(ArrayList<String[]> output){
        System.out.println("Combinations:\n");
        for(String[] test:output){
            for(String combination:test){
                System.out.print(combination);
            }
            System.out.print("\n");
        }

        /*
        Since in the given example there are four sets of 2, 4, 2 and 1 elements, respectively,
        there should be 2 * 4 * 2 * 1 = 16 different combinations
        */
        System.out.println("\nTotal number of combinations: " + output.size());
    }

    public static void main(String[] args){

        String[][] arr = {
            {"a", "b"},
            {"c", "d", "e", "f"},
            {"g", "h"},
            {"i"}};
        getCombinations(arr, true);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.