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.
[]array, or one of the collection classes, the latter being better as you can add to it as you return from each level.