简体   繁体   English

java递归创建具有所有可能组合的列表的列表

[英]java recursion create list of lists with all possible combinations

Here is the problem: I have a number of lists. 问题出在这里:我有很多列表。 To start, I simply choose lists of String, let us take 2 simple ones: [A,B,C] and [W,X,Y,Z] I need to fill in my permutationResult that is an ArrayList of ArrayList such that: 首先,我只选择String列表,让我们看两个简单的列表: [A,B,C][W,X,Y,Z]我需要填写permutationResult ,它是ArrayList的ArrayList,这样:

permutationResult  = [[A, W], [A, X], [A, Y], [A, Z], [B, W], [B, X], [B, Y], [B, Z], [C, W], [C, X], [C, Y], [C, Z]]

I manage to get the combinations by recursion, but when I try to store the results in my permutationResult List, this list seems to be completely erased and replaced by the last permutation at each time. 我设法通过递归来获得组合,但是当我尝试将结果存储在permutationResult列表中时,此列表似乎已被完全擦除,并每次都被最后的排列替换。 I copy my code below and the result of code running. 我在下面复制我的代码和代码运行的结果。 I added some System.out.println in order to notice where it goes wrong, but I can't figure out what to do so any help is welcome. 我添加了一些System.out.println以便注意哪里出错了,但是我不知道该怎么做,因此欢迎任何帮助。 Thank you in advance. 先感谢您。 (The execution of this code is also below) (此代码的执行也在下面)

public void permute(ArrayList<ArrayList<String>> all_Lists, ArrayList<ArrayList<String>> permutationResult, ArrayList<String> objectPutInList, int indexOfList) {
        if ((indexOfList == all_Lists.size()) && (objectPutInList.size() == all_Lists.size())) {
            permutationResult.add(objectPutInList);
            System.out.println("-----------> : "+objectPutInList);
            System.out.println("put in index : "+permutationResult.lastIndexOf(objectPutInList));
            System.out.println("combinations are : "+permutationResult);
            objectPutInList.remove(objectPutInList.size() - 1);
            System.out.println("2 combinations are : "+permutationResult);
            System.out.println("");
            return;
        }
        for (int i = 0; i < all_Lists.get(indexOfList).size(); ++i) 
        {
            objectPutInList.add(all_Lists.get(indexOfList).get(i));
            permute(all_Lists, permutationResult, objectPutInList, indexOfList + 1);
        }
        if (objectPutInList.size() != 0){
            objectPutInList.remove(objectPutInList.size() - 1);
        }
        return;
    }

Here is the execution: 这是执行:

-----------> : [A, W]
put in index : 0
combinations are : [[A, W]]
2 combinations are : [[A]]

-----------> : [A, X]
put in index : 1
combinations are : [[A, X], [A, X]]
2 combinations are : [[A], [A]]

-----------> : [A, Y]
put in index : 2
combinations are : [[A, Y], [A, Y], [A, Y]]
2 combinations are : [[A], [A], [A]]

-----------> : [A, Z]
put in index : 3
combinations are : [[A, Z], [A, Z], [A, Z], [A, Z]]
2 combinations are : [[A], [A], [A], [A]]

-----------> : [B, W]
put in index : 4
combinations are : [[B, W], [B, W], [B, W], [B, W], [B, W]]
2 combinations are : [[B], [B], [B], [B], [B]]

-----------> : [B, X]
put in index : 5
combinations are : [[B, X], [B, X], [B, X], [B, X], [B, X], [B, X]]
2 combinations are : [[B], [B], [B], [B], [B], [B]]

-----------> : [B, Y]
put in index : 6
combinations are : [[B, Y], [B, Y], [B, Y], [B, Y], [B, Y], [B, Y], [B, Y]]
2 combinations are : [[B], [B], [B], [B], [B], [B], [B]]

-----------> : [B, Z]
put in index : 7
combinations are : [[B, Z], [B, Z], [B, Z], [B, Z], [B, Z], [B, Z], [B, Z], [B, Z]]
2 combinations are : [[B], [B], [B], [B], [B], [B], [B], [B]]

-----------> : [C, W]
put in index : 8
combinations are : [[C, W], [C, W], [C, W], [C, W], [C, W], [C, W], [C, W], [C, W], [C, W]]
2 combinations are : [[C], [C], [C], [C], [C], [C], [C], [C], [C]]

-----------> : [C, X]
put in index : 9
combinations are : [[C, X], [C, X], [C, X], [C, X], [C, X], [C, X], [C, X], [C, X], [C, X], [C, X]]
2 combinations are : [[C], [C], [C], [C], [C], [C], [C], [C], [C], [C]]

-----------> : [C, Y]
put in index : 10
combinations are : [[C, Y], [C, Y], [C, Y], [C, Y], [C, Y], [C, Y], [C, Y], [C, Y], [C, Y], [C, Y], [C, Y]]
2 combinations are : [[C], [C], [C], [C], [C], [C], [C], [C], [C], [C], [C]]

-----------> : [C, Z]
put in index : 11
combinations are : [[C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z], [C, Z]]
2 combinations are : [[C], [C], [C], [C], [C], [C], [C], [C], [C], [C], [C], [C]]

How about the following recursive solution? 以下递归解决方案如何? You can make use of Set in order to remove duplicates. 您可以使用Set来删除重复项。 The idea here mimics two for loops. 这里的想法模仿了两个for循环。 You iterate over first list 您遍历第一个列表

go(a.subList(1, a.size()), b, acc);

and then through the second 然后通过第二

go(a, b.subList(1, b.size()), acc);

Please keep in mind that Java is rarely considered as first-choice language for recursive problems. 请记住,Java很少被视为递归问题的首选语言。

public class Perm {

    public List<List<String>> perm(List<String> a, List<String> b) {
        Set<List<String>> acc = new HashSet<>();
        go(a, b, acc);
        return new LinkedList<>(acc);
    }

    private void go(List<String> a, List<String> b, Set<List<String>> acc) {
        if (a.size() == 0 || b.size() == 0) {
            return;
        }
        List<String> aa = new LinkedList<>();
        aa.add(a.get(0));
        aa.add(b.get(0));
        acc.add(aa);

        go(a.subList(1, a.size()), b, acc);
        go(a, b.subList(1, b.size()), acc);
    }

    public static void main(String[] args) {
        List<String> a = new LinkedList<>();
        a.add("X");
        a.add("Y");
        a.add("Z");

        List<String> b = new LinkedList<>();
        b.add("A");
        b.add("B");
        b.add("C");
        b.add("D");
        System.out.println(new Perm().perm(a, b));
    }
}

Well, I guess I figured it out! 好吧,我想我已经知道了! Here is the answer to my question in case we have one or several lists: Thank you slawekpl for your prompt reply yesterday. 如果我们有一个或几个列表,这是我的问题的答案:谢谢slawekpl昨天的迅速答复。 PS: This is the first time I post a question on this platform, so I apologize if I don't know how to display properly my codes :) PS:这是我第一次在此平台上发布问题,因此,如果我不知道如何正确显示我的代码,我深表歉意。

public class TestObject {

    static ArrayList<ArrayList<Object>> permute(Object val, ArrayList<ArrayList<Object>> all_Lists) {

        ArrayList<ArrayList<Object>> permuteOneList;
        ArrayList<ArrayList<Object>> permuteSeveralLists = new ArrayList<ArrayList<Object>>();

        if (all_Lists.size() != 1) {
            for (int i = 0; i < all_Lists.get(0).size(); i++) {
                permuteOneList = permute(all_Lists.get(0).get(i), new ArrayList(all_Lists.subList(1, all_Lists.size())));
                if (!val.equals("")) {
                    ArrayList<Object> comb;
                    for (int j = 0; j < permuteOneList.size(); j++) {
                        comb = permuteOneList.get(j);
                        comb.add(0, val);
                        permuteSeveralLists.add(comb);
                    }
                } else {
                    permuteSeveralLists.addAll(permuteOneList);
                }
            }
            return permuteSeveralLists;
        } 
        else {
            permuteOneList = new ArrayList<ArrayList<Object>>();
            for (int i = 0; i < all_Lists.get(0).size(); i++) {
                ArrayList<Object> comb = new ArrayList<Object>();
                if (val ==""){
                    comb.add(all_Lists.get(0).get(i));
                    permuteOneList.add(comb);
                }
                else {
                    comb.add(val);
                    comb.add(all_Lists.get(0).get(i));
                    permuteOneList.add(comb);
                }
            }
            return permuteOneList;
        }

    }

    public static void main(String[] args) {
        ArrayList<Object> l1 = new ArrayList<Object>();
        l1.add("a");
        l1.add("b");
        l1.add("c");
        l1.add("d");
        ArrayList<Object> l2 = new ArrayList<Object>();
        l2.add("w");
        l2.add("x");
        l2.add("y");
        l2.add("z");
        ArrayList<ArrayList<Object>> all_Lists = new ArrayList<ArrayList<Object>>();

        ArrayList<Object> l3 = new ArrayList<Object>();
        l3.add("1");
        l3.add("2");
        l3.add("3");
        all_Lists.add(l1);
        all_Lists.add(l2);
        all_Lists.add(l3);
        ArrayList<ArrayList<Object>> permt = permute("", all_Lists);
        System.out.println("size : " + permt.size());
        System.out.println("permutation is : " + permt);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM