2013-02-07 139 views
2

海蘭傢伙, 我有這樣的事情:Java - 遞歸排列?

String keyBase = "keyA"; 
    String[] valueBase = {"a1","a2","a3"}; 

    String keyLevel1 = "keyB"; 
    String[] valueLevel1 = {"b1","b2","b3"}; 

    String keyLevel2 = "keyC"; 
    String[] valueLevel2 = {"c1","c2","c3"}; 

而且需要是這樣的:

String[][] f1 = {{"keyA","a1"},{"keyB","b1"}{"keyC","c1"}} 
String[][] f2 = {{"keyA","a1"},{"keyB","b1"}{"keyC","c2"}} 
String[][] f2 = {{"keyA","a1"},{"keyB","b1"}{"keyC","c3"}} 

String[][] f5 = {{"keyA","a1"},{"keyB","b2"}{"keyC","c1"}} 
String[][] f6 = {{"keyA","a1"},{"keyB","b2"}{"keyC","c2"}} 
... 

什麼是簡單的,但好辦法做到這一點?

感謝

+7

這是一組交叉積,而不是一個置換。你有什麼嘗試? – UmNyobe

+0

我會從Guava方法Collections2.permutations – WannaKnow

+0

開始...而對於笛卡爾產品部分,還有一個Guava方法:Sets.cartesianProduct – WannaKnow

回答

0

我不知道遞歸排列。
我剛剛嘗試根據您的輸出。

for(int i = 0 ; i <3 ; i++) { 
    for(int j = 0 ; j <3 ; j++) { 
     for(int k = 0 ; k <3 ; k++) { 
     system.out.println(keyBase +","+ valueBase[i] +","+ 
          keyLevel1 +","+ valueLevel1[j] +","+ 
          keyLevel2 +","+ valueLevel2[k]) 
     } 
    } 
}