package com.lagou.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @descriptions:
* @author: FUQIANG.ZHOU
* @date: 2022/6/28 23:14
* @version: 1.0
*/
public class AirticketTest {
public static void main(String[] args) {
String[] seatType ={"A1","A2","E"};
String[] rangeType ={"D","N"};
String[] seasonType ={"P","N","L"};
String[] discountType ={"N","D","J","S"};
List<List<String>> listData = new ArrayList<>();
listData.add(Arrays.asList(seatType));
listData.add(Arrays.asList(rangeType));
listData.add(Arrays.asList(seasonType));
listData.add(Arrays.asList(discountType));
List<List<String>> lisReturn = getDescartes(listData);
//System.out.println(lisReturn);
for( List<String> item : lisReturn)
{
System.out.println(item);
}
}
private static <T> List<List<T>> getDescartes(List<List<T>> list) {
List<List<T>> returnList = new ArrayList<>();
descartesRecursive(list, 0, returnList, new ArrayList<>());
return returnList;
}
/**
* 递归实现
* 原理:从原始list的0开始依次遍历到最后
*
* @param originalList 原始list
* @param position 当前递归在原始list的position
* @param returnList 返回结果
* @param cacheList 临时保存的list
*/
private static <T> void descartesRecursive(List<List<T>> originalList, int position, List<List<T>> returnList, List<T> cacheList) {
List<T> originalItemList = originalList.get(position);
for (int i = 0; i < originalItemList.size(); i++) {
//最后一个复用cacheList,节省内存
List<T> childCacheList = (i == originalItemList.size() - 1) ? cacheList : new ArrayList<>(cacheList);
childCacheList.add(originalItemList.get(i));
if (position == originalList.size() - 1) {//遍历到最后退出递归
returnList.add(childCacheList);
continue;
}
descartesRecursive(originalList, position + 1, returnList, childCacheList);
}
}
}