2014-01-30 25 views
4

假设我有3个列表:['q','w'],['a','s'],['z','x']。如何从这些列表中获取可能的组合列表?所以我得到一个清单[['q','a','z'],['q','s','z']]等等。我做的方法有两个,但找不出一个N个清单:获取列表元素的组合列表

static <E> ArrayList combine(ArrayList<E> one,ArrayList<E> two) 
{ 
    ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); 
    for(E e:one) 
    { 
     for(E e2:two) 
     { 
      ArrayList ps=new ArrayList(); 
      ps.add(e); 
      ps.add(e2); 
      combs.add(ps); 
     } 
    } 
    return combs; 
} 

我发现,这是由番石榴的Sets.cartesianProduct完成。

回答

2

你需要N个嵌套循环,这是很难的。

尽管你可以使用递归来实现这一点。

static <E> ArrayList combine(ArrayList<E> soFar, ArrayList<E>... lists) 
{ 
    // Rather than constantly making and remaking this list could just use one 
    // and pass it around and add stuff to it. This works though. 
    ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); 

    // Loop through the first list looking for elements 
    for(E e:lists[0]) 
    { 
     // Create a new List to build this combination 
     ArrayList<E> temp = new ArrayList<>(soFar); 
     // Add this element to the combination 
     temp.add(e); 
     // If there are more lists recurse down 
     if (lists.length > 1) { 
      // Use recursion to add all combinations of the remaining lists 
      combs.addAll(combine(temp, lists.subList(1))); 
     } else { 
      // There are no more lists so we are done, add temp to the combos 
      combs.add(temp); 
     } 
    } 
    return combs; 
} 


// Call this method to start things going, the other one should probably be private 
static <E> ArrayList combine(ArrayList<E>... lists) 
    return combine(new ArrayList<E>(), lists); 
} 
+0

你如何调用lists.subList?这不是一个数组吗? – Alexiy

+0

好点,这里的所有代码都是直接输入的,所以需要进行一些调整。 :)使用http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange%28T[],%20int,%20int%29或只是传递完整的数组使用soFar.size()作为数组的索引。 (第二种方法会更有效率)。 –

0

了,你可以创建一个内部类构造函数(参数...),所以你可以把这个类的一个列表,它处理所有的组合

1

你可能想看看类https://github.com/javagl/Combinatorics/blob/master/src/main/java/de/javagl/utils/math/combinatorics/MixedRangeCombinationIterable.java( “独立” 级,只是&插入复制到项目)

用法示例:

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class Combinations 
{ 
    public static void main(String[] args) 
    { 
     List<List<Character>> lists = new ArrayList<List<Character>>(); 
     lists.add(Arrays.asList('q','w')); 
     lists.add(Arrays.asList('a','s')); 
     lists.add(Arrays.asList('z','x')); 

     MixedRangeCombinationIterable<Character> iterable = 
      new MixedRangeCombinationIterable<Character>(lists); 
     for (List<Character> element : iterable) 
     { 
      System.out.println(element); 
     } 
    } 
} 

您是实际LY计算输入的http://en.wikipedia.org/wiki/Cartesian_product的元素设置

5

专为懒人设计(使用番石榴):

Set<List<String>> result = Sets.cartesianProduct(
       ImmutableSet.of("q", "w"), 
       ImmutableSet.of("a", "s"), 
       ImmutableSet.of("z", "x") 
     ); 

System.out.println(result); 

输出:

[ [q, a, z], [q, a, x], [q, s, z], [q, s, x], [w, a, z], [w, a, x], [w, s, z], [w, s, x] ]