Java -从递归函数返回

时间:2021-11-26 18:03:59

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:

如果我输入{x, y,z}它输出到控制台:

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.

我的问题是,我找不到将这些值返回到原始调用函数的方法。所以我希望这个函数不返回'void'而是一个'String',包含sPrefix的concatated值。

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

我已经为此挣扎了一段时间了,我似乎再也看不清楚了。如有任何帮助,我们将不胜感激。

3 个解决方案

#1


3  

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.

然后可以有一个包装器方法来创建新的ArrayList ,将其传递到上面的方法中,并返回它。

#2


1  

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

你可以有一个ArrayList 并添加所有的字符串到它。然后你可以返回这个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;
}

#3


0  

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

一旦基本概念生效,你也可以使用ListArray而不是String。

#1


3  

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.

然后可以有一个包装器方法来创建新的ArrayList ,将其传递到上面的方法中,并返回它。

#2


1  

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

你可以有一个ArrayList 并添加所有的字符串到它。然后你可以返回这个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;
}

#3


0  

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

一旦基本概念生效,你也可以使用ListArray而不是String。