Michiel Meulendijk
Michiel Meulendijk

Reputation: 355

Java - Returning from a recursive function

I'm trying to return all possible permutations of values in a String array. I've come up with the following code making all possible permutations; it works fine.

private void combineArray(String sPrefix, String[] sInput, int iLength) {
    if (iLength == sPrefix.length()) {
        //This value should be returned and concatenated:
        System.out.println(sPrefix);
    } else {
        for (int i=0; i<sInput.length; i++) {
            combineArray(sPrefix.concat(sInput[i]), ArrayUtils.removeElement(sInput, sInput[i]), iLength);
        }
    }
}

If I put in {x, y ,z} it prints to the console:

xyz
xzy
yxz
yzx
zxy
zyx

My problem is that I can't find a way to return these values to the original calling function. So I'd like this function not to return 'void' but a 'String' containing the concatened values of sPrefix.

I've been struggling with this for a while now and I can't seem to see clearly anymore. :) Any help would be appreciated.

Upvotes: 3

Views: 4852

Answers (3)

srini.venigalla
srini.venigalla

Reputation: 5145

Keep appending to the same output.. Like this:

private String combineArray(String sPrefix, String[] sInput, int iLength, String output) {
    if (iLength == sPrefix.length()) {
        //This value should be returned and concatenated:
        System.out.println(sPrefix);
        output = output+"|+sPrefix;
        return output;
    } else {
        for (int i=0; i<sInput.length; i++) {
            output = combineArray(sPrefix.concat(sInput[i]), ArrayUtils.removeElement(sInput, sInput[i]), iLength, output);
        }
    }
}

You can also use a ListArray instead of a String, once the basic concept works..

Upvotes: -1

ruakh
ruakh

Reputation: 183301

Rather than returning a list, I think it might work better to pass in a list as an argument, and populate it inside the method:

private void combineArray(List<String> lOut, String sPrefix, String[] sInput, int iLength) {
    if (iLength == sPrefix.length()) {
        //This value should be returned and concatenated:
        System.out.println(sPrefix);
        lOut.add(sPrefix);
    } else {
        for (int i=0; i<sInput.length; i++) {
            combineArray(lOut, sPrefix.concat(sInput[i]), ArrayUtils.removeElement(sInput, sInput[i]), iLength);
        }
    }
}

You can then have a wrapper method that creates the new ArrayList<String>, passes it into the above method, and returns it.

Upvotes: 4

Rohit Jain
Rohit Jain

Reputation: 213243

You can have an ArrayList<String> and add all the strings to it.. And then you can return this ArrayList..

List<String> listString = new ArrayList<>();
private void combineArray(String sPrefix, String[] sInput, int iLength) {
    if (iLength == sPrefix.length()) {
        listString.add(sPrefix);
        //This value should be returned and concatenated:
        System.out.println(sPrefix);
    } else {
        for (int i=0; i<sInput.length; i++) {
            combineArray(sPrefix.concat(sInput[i]), ArrayUtils.removeElement(sInput, sInput[i]), iLength);
        }
    }
    return listString;
}

Upvotes: 1

Related Questions