package mytest;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CombineTest {
public static void main(String[] args) {
String[][] arr = new String[3][];
for (int i = 0; i < 3; i++) {
String[] a = new String[4];
for (int j = 0; j < 4; j++) {
a[j] = RandomUtil.randomString(5);
}
arr[i] = a;
}
System.out.println(JSONUtil.toJsonStr(arr));
String[] item = new String[arr.length];
List<String[]> result = new ArrayList<>();
combine(arr, 0, item, result);
for (String[] a : result) {
System.out.println(JSONUtil.toJsonStr(a));
}
}
private static void combine(String[][] arr, int i, String[] item, List<String[]> result) {
if (i + 1 < arr.length) {
for (int j = 0; j < arr[i].length; j++) {
String[] rs = Arrays.copyOf(item, item.length);
rs[i] = arr[i][j];
combine(arr, i + 1, rs, result);
}
} else {
for (int j = 0; j < arr[i].length; j++) {
String[] rs = Arrays.copyOf(item, item.length);
rs[rs.length - 1] = arr[i][j];
result.add(rs);
}
}
}
}
java中多个数组元素进行排列组合
最新推荐文章于 2023-11-16 23:46:03 发布
该文章展示了一段Java代码,它利用Hutool库的RandomUtil生成随机字符串数组,并通过递归方法`combine`进行组合测试。主要操作包括初始化多维字符串数组,将数组内容转换为JSON字符串,以及遍历和打印所有可能的字符串组合。

2688

被折叠的 条评论
为什么被折叠?



