当前位置: 代码迷 >> 综合 >> (算法笔记)排列组合---递归实现
  详细解决方案

(算法笔记)排列组合---递归实现

热度:56   发布时间:2024-01-26 18:48:13.0
package CollectionDemo;
//Java实现排列组合--递归
//参考:https://blog.csdn.net/chunrudikai/article/details/60958993?utm_source=app
import java.util.ArrayList;
import java.util.List;public class PailiexuheDemo {static ArrayList<String> arrayList1=new ArrayList<>();public static void main(String[] args) {String[] color = {"红", "蓝", "黑"};String[] size = {"S", "M", "L"};String[] other = {"a", "b"};List<String[]> list = new ArrayList<String[]>();list.add(color);list.add(size);list.add(other);sorting(list, color, "");for(String str:arrayList1){System.out.println(str);}}public static void sorting(List<String[]> list, String[] arrs, String str) {for (int i = 0; i < list.size(); i++) {//System.out.println(list.indexOf(arrs));//取得当前的数组if (i == list.indexOf(arrs)) {// 迭代数组for (String arr : arrs) {arr = str + arr;if (i < list.size() - 1) {arr = arr + ",";sorting(list, list.get(i + 1), arr);//System.out.println("回到了这里");} else if (i == list.size() - 1) {//System.out.println(arr);arrayList1.add(arr);}}}}}}

主要参考:参考:https://blog.csdn.net/chunrudikai/article/details/60958993?utm_source=app