這是問題:我有一些列表。首先,我只是選擇字符串列表,讓我們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