在JAVA中,从不同数量的数组和数组元素生成子集
我有如下数组在JAVA中,从不同数量的数组和数组元素生成子集,java,arrays,Java,Arrays,我有如下数组 A:[[1,2,3],[100,200]] B:[[4,5],[300,400],[500,600,700]] C:[[6,7,8,9]] 现在我必须使用上面的数组元素进行设置 Set1:[[1,2,3],[4,5],[6,7,8,9]] Set2:[[1,2,3],[300,400],[6,7,8,9]] Set3:[[1,2,3],[500,600,700],[6,7,8,9]] Set4:[[100,200],[4,5],[6,7,8,9]] Set5:[[100,200
A:[[1,2,3],[100,200]]
B:[[4,5],[300,400],[500,600,700]]
C:[[6,7,8,9]]
现在我必须使用上面的数组元素进行设置
Set1:[[1,2,3],[4,5],[6,7,8,9]]
Set2:[[1,2,3],[300,400],[6,7,8,9]]
Set3:[[1,2,3],[500,600,700],[6,7,8,9]]
Set4:[[100,200],[4,5],[6,7,8,9]]
Set5:[[100,200],[300,400],[6,7,8,9]]
Set6:[[100,200],[500,600,700],[6,7,8,9]]
在这里,我希望代码是动态的,就像数组的数量可能会改变,以及每个数组中的元素数量一样。这里我只是用三个数组来解释
下面是我尝试过的代码,但它不是动态的。下面的代码可以解决这个问题,但是如果数组的数量增加,那么我必须手动更改代码,并且必须为循环添加更多。我如何克服这个问题
List<Integer> setList = new ArrayList<>;
for (int j = 0; j < 4; j++) {
for (int k = 0; k < A.length; k++) {
for (int l = 0; l < B.length; l++) {
for (int m = 0; m < C.length; m++) {
List<Integer> tempList = new ArrayList<>
tempList.add(A[k]);
tempList.add(B[l]);
tempList.add(C[m]);
setList.add(tempList);
}
}
}
}
List setList=newarraylist;
对于(int j=0;j<4;j++){
for(int k=0;k
这就是您所需要的:)
int[][]A=新的int[][{{1,2,3},{100200};
int[][]B=新的int[][{{4,5},{300400},{500600700};
int[][]C=新int[][{{6,7,8,9}};
List setList=new ArrayList();
for(int i=0;i
您可以将原始数据建模为三维阵列
int [][][] arrays = new int[][][] {
{{1,2,3}, {100, 200}}, //array A
{{4,5}, {300, 400}, {500, 600, 700}},//array B
{{6,7,8,9}} //array C
};
如果要添加新行(除了a
、B
、C
),只需添加新行即可
public static void solve(int[][][] arrays, List<List<List<Integer>>> result, List<List<Integer>> current,
int row) {
if (row == arrays.length) {
result.add(current);
return;
}
for (int j = 0; j < arrays[row].length; j++) {
List<List<Integer>> localCurrent = new ArrayList<>(current); //Copy the previous result
List<Integer> currentData = Arrays.stream(arrays[row][j])
.boxed()
.collect(Collectors.toList()); //Convert current int[] to List<Integer>
localCurrent.add(currentData);
solve(arrays, result, localCurrent, row + 1);
}
}
publicstaticvoidsolve(int[]数组,列表结果,列表当前,
整数行){
if(行==数组.length){
结果。添加(当前);
返回;
}
对于(int j=0;j
//对于前面提到的int[][]数组
列表结果=新建ArrayList();
当前列表=新的ArrayList();
求解(数组、结果、当前、0);
对于(int i=0;i
您需要使用recursion@JoseDaSilva我如何在递归中做到这一点?你知道递归吗?
public static void solve(int[][][] arrays, List<List<List<Integer>>> result, List<List<Integer>> current,
int row) {
if (row == arrays.length) {
result.add(current);
return;
}
for (int j = 0; j < arrays[row].length; j++) {
List<List<Integer>> localCurrent = new ArrayList<>(current); //Copy the previous result
List<Integer> currentData = Arrays.stream(arrays[row][j])
.boxed()
.collect(Collectors.toList()); //Convert current int[] to List<Integer>
localCurrent.add(currentData);
solve(arrays, result, localCurrent, row + 1);
}
}
//For int [][][] arrays mentioned eariler
List<List<List<Integer>>> result = new ArrayList<>();
List<List<Integer>> current = new ArrayList<>();
solve(arrays, result, current, 0);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
[[1, 2, 3], [4, 5], [6, 7, 8, 9]]
[[1, 2, 3], [300, 400], [6, 7, 8, 9]]
[[1, 2, 3], [500, 600, 700], [6, 7, 8, 9]]
[[100, 200], [4, 5], [6, 7, 8, 9]]
[[100, 200], [300, 400], [6, 7, 8, 9]]
[[100, 200], [500, 600, 700], [6, 7, 8, 9]]