package dumingwei.t;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GroupUtil {
public static void main(String[] args) {
Integer [] arr1= { 1, 2,3};
Integer [] arr2= { 4, 5,6};
Integer [] arr3= { 7, 8};
Integer [] arr4= { 10, 11,12};
List<Integer[]> list = get(new Integer[][]{arr1,arr2,arr3,arr4},new Integer[4]);
for (Integer[] integers : list) {
System.out.println(Arrays.toString(integers));
}
}
private static <T> List<T[]> get(T[][] doubleArr,T [] storageArr) {
int length = length(doubleArr);
List<T[]> list = new ArrayList<>(length);
searchNext(doubleArr, storageArr, 0,list);
return list;
}
private static <T> int length(T [][] doubleArr) {
int i = 1;
for (T[] arr : doubleArr) {
i = i * arr.length;
}
return i;
}
private static <T> void searchNext(T [][] doubleArr,T [] storageArr,int currentArrIndex,List<T[]> list) {
T[] arr = doubleArr[currentArrIndex];
for (int i = 0; i < arr.length; i++) {
storageArr[currentArrIndex] = arr[i];
if (currentArrIndex == doubleArr.length -1){
//如果到达最大
T[] copyOf = Arrays.copyOf(storageArr, doubleArr.length);
list.add(copyOf);//把数据拿到.
} else {
currentArrIndex++;
searchNext(doubleArr, storageArr, currentArrIndex,list);
currentArrIndex--;
}
}
}
}