将列表<string []>的每个元素相互交叉</string []>

时间:2012-11-03 23:56:43

标签: java algorithm

我试图将包含String []的列表中的每个元素相互交叉。输入列表中的每个元素(String[])的长度为3或4

Input: [{'J', 'K', 'L'}, {'G', 'H', 'I'}]
Output: ["JG", "JH", "JI", 
         "KG", "KH", "KI", 
         "LG", "LH", "LI"]

Input: [{'J', 'K', 'L'}, {'G', 'H', 'I'}, {'A', 'B', 'C'}]
Output: ["JGA", "JGB", "JGC", 
         "KGA", "KGB", "KGC", 
         "LGA", "LGB", "LGC", 
         "JHA", "JHB", "JHC",
         "KHA", "KHB", "KHC", 
         "LHA", "LHB", "LHC", 
         "JIA", "JIB", "JIC",
         "KIA", "KIB", "KIC",
         "LIA", "LIB", "LIC"]

输出中每个元素的大小等于输入列表中的总元素。

我已完成以下操作,但未获得正确的结果。

ArrayList<String> output = new ArrayList();
for (String [] s : InputList)
   for (int i = 0; i < s.length; i++) {
      if (output.size < 3)
         output.add(s[i])
      else {
         output.add(output.get(i)+s[i]);
      }
   }
}

2 个答案:

答案 0 :(得分:1)

您可以使用递归方法。这适用于任何(合理)大小的列表:

public static List<String> combine(List<String[]> list) {
    List<String> elements = new ArrayList<String>();
    if (!list.isEmpty()) {
        String[] head = list.get(0);
        List<String> tail;
        if (list.size() > 1) {
            tail = combine(list.subList(1, list.size()));
        } else {
            tail = Arrays.asList("");
        }
        for (String headElem : head) {
            for (String tailElem : tail) {
                elements.add(headElem + tailElem);
            }
        }
    }
    return elements;
}

答案 1 :(得分:0)

如果您仍然感兴趣,这是一个非递归版本:

public String[] cartesian(char input[][]) {                 
    List<String> output = new ArrayList<String>();          
    int m = input.length;                                   
    int n = input[0].length;                                
    int c[] = new int[m];                                   
    int i = m - 1;                                          
    do {                                                    
        output.add(getElement(input, c, m));                
        do {                                                
            if (c[i] < n - 1) {                             
                c[i]++;                                     
                if (i < m - 1)                              
                    i++;                                    
                break;                                      
            } else {                                        
                c[i] = 0;                                   
                i--;                                        
            }                                               
        } while (i >= 0);                                   
    } while (i >= 0);                                       
    return output.toArray(new String[output.size()]);       
}                                                           

private String getElement(char[][] list, int[] c, int n) { 
    String element = "";                                    
    for (int i = 0; i < n; i++)                             
        element += list[i][c[i]];                           
    return element;                                         
}                                                          

您应该可以使用任意数量的子列表。对于您的方案,您可以这样做:

char[][] input={{'J', 'K', 'L'}, {'G', 'H', 'I'}, {'A', 'B', 'C'}};
String[] output = cartesian(input);