2

I have the following bunch of arrays:

public class ArrayPermutationExample {
    private static final String PREFIX = "ignore(FAILURE) { build(\"load\", ";
    public static final String ENDING = ")}";
    private static String[] arr_1 = new String[] {
        "111",
        "222",
        "333"};

    private static String[] arr_2 = new String[]{
        "aaa",
        "bbb",
        "ccc"};

    private static String[] arr_3 = new String[] {
        "***",
        "&&&",
        "$$$"};

I need to find permutation with other arrays, excluding native array.
The output should look like:

111aaa
111bbb
111ccc
111***
111&&&
111$$$

222aaa
222bbb
222ccc
...

333aaa
333bbb
333ccc
...

Finally, for all those permutations should be added prefix and ending:

prefix permutation string endings

And at the end we should have something like:

ignore(FAILURE) { build("load", 111aaa )}

I completely stuck with a solution for this task:

private static void processArrays(String[] ... arrays) {
    ArrayList<String> result = new ArrayList<>();
    for (String[] array : arrays) {
        String[] currentArray = array;
        for (String line : currentArray) {
            // exclude all lines from current array & make concatenation with every line from others
        }
    }
}

How to solve this issue?

UPDATE:

I want to add that finally, we need to have a distinct list without any dublications. Even following example will be duplicating each other:

111aaa***
***111aaa

I believe that this task should have a solution with Java 8 style.

5
  • May I knew the reason for downvoting? Commented Jul 20, 2017 at 19:27
  • At first glance your example looks pretty complicated, can you provide a smaller example? Will be easier to read and obtain a good overview of your question. Commented Jul 20, 2017 at 19:31
  • @Zabuza I updated question Commented Jul 20, 2017 at 19:43
  • 1
    It is better now, thanks :) Where is the problem in just using an existing permutation builder (there are multiple solutions at SO) and modify it a bit? I think that would also help understanding how this problem can be solved, first take a look at how you would do it in general. Commented Jul 20, 2017 at 19:57
  • @Zabuza I wondering to know how to use Java 8 style for solving this issue. Commented Jul 20, 2017 at 20:01

2 Answers 2

2

I think this may be a combination since you don't actually care about getting all orderings (other than printing in the order specified by the order of the array parameters), but regardless, here is the code I wrote. I used a stack for storing unfinished arrays. Pushing to the stack each possibility at any given point for every array and pushing to results for any completed array.

public static List<String> getCombinations(String prefix, String ending, String[]... arrays) {
    List<String> results = new ArrayList<>();
    Stack<String[]> combinations = new Stack<>();
    combinations.add(new String[arrays.length]);

    while (!combinations.isEmpty()) {
        String[] currentArray = combinations.pop();

        if (currentArray[arrays.length - 1] == null) {
            for (int i = 0; i < arrays.length; i++) {
                if (currentArray[i] == null) {
                    for (int j = 0; j < arrays[i].length; j++) {
                        String[] newArray = currentArray.clone();
                        newArray[i] = arrays[i][j];
                        combinations.add(newArray);
                    }

                    break;
                }

            }
        } else {
            StringBuilder stringBuilder = new StringBuilder(prefix);
            for (String string : currentArray) {
                stringBuilder.append(string);
            }
            stringBuilder.append(ending);
            results.add(stringBuilder.toString());
        }
    }

    return results;
}

You would just need to iterate over the returned list to print out all of the strings.

As an added note, this method could be written recursively, but I usually like using a stack instead of using recursion when possible because recursion can be slow sometimes.

Sign up to request clarification or add additional context in comments.

3 Comments

Your return list contains duplication. One permutation string is repeated 6 times. We should have distinct results.
you can get rid of this by using HashSet instead of ArrayList Set<String> results = new HashSet<>();. Finally, you will get distinct results without any duplication.
I have modified my answer, but I didn't use a HashSet. Seems a bit hacky to me. I'd rather just have it never generate duplicates in the first place.
1

The way I read it: the first array arr_1 is prepended to the next 3 arrays. I think the prefix is "ignore(FAILURE) { build("load", " and the ending is "}}"

String prefix = "ignore(FAILURE) { build(\"load\", ";
String ending = "}}";
for (String first: arr_1) {
    for (String second: arr_2) {
       System.out.println( prefix + first + second + ending);
    }
    for (String second: arr_3) {
       System.out.println( prefix + first + second + ending);
    }
    for (String second: arr_4) {
       System.out.println( prefix + first + second + ending);
    }
}

1 Comment

Right direction! Even more, after the first array -> the same way should be processed the second array -> after it third array -> and finally the last one. However, I want to find how to create some smart way for such combinatorics instead of procedural style for each array.

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.