这是问题:我有一些列表。首先,我只是选择字符串列表,让我们2分简单的:[A,B,C]
和[W,X,Y,Z]
我需要填写我的permutationResult
是ArrayList中的ArrayList这样的:java递归创建列表与所有可能的组合
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]]
我设法得到的组合通过递归,但是当我尝试将结果存储在我的permutationResult
列表中时,此列表似乎已被完全擦除,并被每次最后一次置换所取代。我复制下面的代码和代码运行的结果。我添加了一些System.out.println
为了注意它出错的地方,但我不知道该怎么做,所以我们欢迎任何帮助。 预先感谢您。 (此代码的执行也低于)
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;
}
下面是执行:
-----------> : [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]]
感谢您的快速答复!然而,这种方法只需要2个列表,我的问题的目的是处理动态数量的列表。任何见解? – NolaBel