3

我正在尝试将包含彼此的列表中的每个元素相交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]);
      }
   }
}
4

2 回答 2

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;
}
于 2012-11-04T00:40:12.990 回答
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);                                 
于 2012-11-03T23:58:55.913 回答