假设我有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;
}
我发现这是由Guava的Sets.cartesianProduct完成的。
答案 0 :(得分: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]
]
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}
}
}
您实际上是在计算输入集{/ 3}}的元素
答案 3 :(得分:0)
up你可以使用构造函数(Parameters ...)创建一个内部类,这样就可以放置一个处理所有组合的类的列表