题目
例如
输入:
123
123
123
输出:
[[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3],[1,3,1],[1,3,2],[1,3,3],[2,1,1],[2,1,2],[2,1,3],[2,2,1],[2,2,2],[2,2,3],[2,3,1],[2,3,2],[2,3,3],[3,1,1],[3,1,2],[3,1,3],[3,2,1],[3,2,2],[3,2,3],[3,3,1],[3,3,2],[3,3,3]]
代码
public static void main(String[] args) {
List<List<Integer>> list = new ArrayList<>();
List<Integer> integers = Arrays.asList(1, 2, 3);
List<Integer> integers1 = Arrays.asList(1, 2, 3);
List<Integer> integers2 = Arrays.asList(1, 2, 3);
list.add(integers);
list.add(integers1);
list.add(integers2);
List<List<Integer>> preList = new ArrayList<>();
List<List<Integer>> curList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
List<Integer> list1 = list.get(i);
preList = new ArrayList<>(curList);
curList = new ArrayList<>();
for (Integer integer : list1) {
if (i == 0) {
List<Integer> list2 = new ArrayList<>();
list2.add(integer);
curList.add(list2);
} else {
for (List<Integer> list21 : preList) {
List<Integer> list2 = new ArrayList<>();
list2.add(integer);
list2.addAll(list21);
curList.add(list2);
}
}
}
}
System.out.println(JSONObject.toJSONString(curList));
}
|