public class Test { public static void main(String[] args) { String[][] dataArray = { {"zhong","chong"},{"qing"},{"hai","huan"},{"ri"}}; combine(dataArray, 4); } public static void combine(String[][] data, int n){ if (n > data.length) {n = data.length;} int[] idx = new int[data.length]; for (int i=0; ilist = new ArrayList<>(); while (idx[0] < 2) { int cnt=0, dig=0; int[] gp = new int[n]; for (int i=0; i < data[gp[0]].length) { StringBuilder sb = new StringBuilder(); for (int i=0; i 0; i--) { if (subIdx[i] == data[gp[i]].length) { subIdx[i] = 0; subIdx[i-1]++; } else { break; } } } } if (dig == n) {break;} idx[data.length-1]++; for (int i=data.length-1; i>0; i--) { if (idx[i] == 2) { idx[i] = 0; idx[i-1]++; } else { break; } } } int count = 0; for (String s : list) { System.out.printf("%s", s); System.out.println(); count++; if (count%10 == 0) {System.out.println();} } System.out.printf("\ncount=%d\n", count); }}
运行结果:
zhongqinghairi
zhongqinghuanri
chongqinghairi
chongqinghuanri
count=4