大家好,想达到如下目的,List1 {"A1", "A2", "A3", "A4"}List2 {"B1", "B2", "B3", "B4", "B5"}List3 {"C1", "C2", "C3", "C4", "C5"}List4 {"D1", "D2", "D3"}List5 {"E1", "E2", "E3", "E4"}List6 {"F1", "F2", "F3", "F4", "F5"}A1,B1,C1,D1,E1,F1A1,B1,C1,D1,E1,F2A1,B1,C1,D1,E1,F3...A1,B1,C1,D1,E2,F1A1,B1,C1,D1,E2,F2A1,B1,C1,D1,E2,F3...实现这样的组合,除了6重循环外,有什么优雅的写法吗?
4 回答
浮云间
TA贡献1829条经验 获得超4个赞
6重循环逻辑上更简单些,不想要这种写法也是可以的,给你个参考吧;
[code="java"]
import java.util.Arrays;
import java.util.List;
public class Mm {
/** * @param args */ public static void main(String[] args) { // int[] a = new int[] { 123, 11 }; List<String>[] t = new List[] { Arrays.asList("1", "2", "3", "4"), Arrays.asList("a", "b", "c"), Arrays.asList("甲", "乙", "丙", "丁") }; int[] pos = new int[t.length]; while (!print(t, pos)) { addpos(t, pos, pos.length - 1); } } public static void addpos(List<String>[] t, int[] pos, int p_length) { if (p_length < 0) return; if (pos[p_length] < t[p_length].size() - 1) { pos[p_length] = pos[p_length] + 1; } else { pos[p_length] = 0; addpos(t, pos, p_length - 1); } } public static boolean print(List<String>[] t, int[] pos) { boolean end = true; for (int i = 0; i < pos.length; i++) { System.out.print(t[i].get(pos[i])); end = end && t[i].size() - 1 == pos[i]; } System.out.println(); return end; }
}
添加回答
举报
0/150
提交
取消