Det er nemt at generalisere.
import java.util.ArrayList;
import java.util.List;
public class Combi {
@FunctionalInterface
public static interface Handler {
public void process(List<String> a1, List<String> a2);
}
public static class Type {
private String prefix;
private int[] nos;
public Type(String prefix, int... nos) {
this.prefix = prefix;
this.nos = nos;
}
public int length() {
return nos.length;
}
public String getElement(int ix) {
return prefix + nos[ix];
}
public List<String> getAllElements() {
List<String> res = new ArrayList<String>();
for(int i = 0; i < length(); i++) {
res.add(getElement(i));
}
return res;
}
}
private static void expand(List<String> base, List<String> a, int ix, List<List<String>> res ) {
for(int i = ix; i < a.size(); i++) {
List<String> newbase = new ArrayList<String>(base);
newbase.add(a.get(i));
res.add(newbase);
expand(newbase, a, i + 1, res);
}
}
private static List<List<String>> expand(Type typ) {
List<List<String>> res = new ArrayList<List<String>>();
List<String> a = typ.getAllElements();
expand(new ArrayList<String>(), a, 0, res);
return res;
}
public static void combine(Type typ1, Type typ2, Handler h) {
for(List<String> a1 : expand(typ1)) {
for(List<String> a2 : expand(typ2)) {
h.process(a1, a2);
}
}
}
}
import java.util.List;
import february.Combi.Type;
public class CombiDemo {
private static String join(List<String> a1, List<String> a2) {
StringBuilder sb = new StringBuilder();
for(String s1 : a1) {
sb.append("," + s1);
}
for(String s2 : a2) {
sb.append("," + s2);
}
return sb.toString().substring(1);
}
private static void print(List<String> a1, List<String> a2) {
System.out.println(join(a1, a2));
}
public static void main(String[] args) {
Combi.combine(new Type("B", 1, 2, 3, 4), new Type("F", 10), CombiDemo::print);
}
}