1

I have arrays like below

A:[[1,2,3],[100,200]] 
B:[[4,5],[300,400],[500,600,700]]
C:[[6,7,8,9]]

Now I have to make sets using above array elements.My expected result should be like

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]]

here I want the code to be dynamic like number of array may change as well as number of elements in each array. Here I just explained with three array .

Here is the code below I have tried but it is not dynamic. the below code can solve the problem but if the number of array increases then I have to change the code manually and have to put more for loops. how can I overcome this issue?

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);

            }
        }
    }
}
3
  • You need to use recursion Commented Mar 15, 2018 at 6:11
  • @JoseDaSilva how can I do that in recursion ? Commented Mar 15, 2018 at 6:15
  • do you know anything about recursion? Commented Mar 15, 2018 at 6:43

2 Answers 2

2

You can model the original data as a 3D array

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
    };

If you want to add a new row (in addition to A, B, C) you just have to add a new row to that.

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]]
Sign up to request clarification or add additional context in comments.

Comments

0

This is what you needed :)

int[][] A = new int[][]{{1,2,3},{100,200}};
int[][] B = new int[][]{{4,5},{300,400},{500,600,700}};
int[][] C = new int[][]{{6,7,8,9}};

List<int[][]> setList = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
    for (int j = 0; j < B.length; j++) {
        for (int k = 0; k < C.length; k++) {
            setList.add(new int[][]{A[i], B[j], C[k]});
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.