Java 具有多个列表的递归置换

Java 具有多个列表的递归置换,java,recursion,methods,Java,Recursion,Methods,我很好奇如何递归地执行我的permuteAndPrintValuesThreeLists\u迭代方法。。。我知道排序数组和执行二进制搜索的基本递归,但我不知道如何使它成为递归方法 我之所以要使用递归,是因为我希望能够添加3个以上的列表,而不需要通过添加另一个for循环来更改我的方法 问题:如何将permuteAndPrintValuesThreeLists方法编写为递归方法 我的输出应该是: 1 1 10 10 100 100 1 1 10 10 200 200 1 1 10 10 300 30

我很好奇如何递归地执行我的
permuteAndPrintValuesThreeLists\u迭代
方法。。。我知道排序数组和执行二进制搜索的基本递归,但我不知道如何使它成为递归方法

我之所以要使用递归,是因为我希望能够添加3个以上的列表,而不需要通过添加另一个for循环来更改我的方法

问题:如何将
permuteAndPrintValuesThreeLists
方法编写为
递归
方法

我的输出应该是:

1 1 10 10 100 100
1 1 10 10 200 200
1 1 10 10 300 300
1 1 20 20 100 100
1 1 20 20 200 200
1 1 20 20 300 300
2 2 10 10 100 100
2 2 10 10 200 200
2 2 10 10 300 300
2 2 20 20 100 100
2 2 20 20 200 200
2 2 20 20 300 300
但事实是:

1 1 10 10 100 100 
200 200 
300 300 
400 400 
20 20 100 100 
200 200 
300 300 
400 400 
3 3 10 10 100 100 
200 200 
300 300 
400 400 
20 20 100 100 
200 200 
300 300 
400 400 

final类问题{
公共静态void main(字符串[]args){
问题p=新问题();
p、 permuteAndPrintValuesThreeLists_Iterative();
}
私有静态列表l1;
私有静态列表l2;
私有静态列表l3;
私人问题(){
l1=新的ArrayList();
l1.添加(新的int[]{1,1});
l1.add(新的int[]{2,2});
l2=新的ArrayList();
l2.add(新的int[]{10,10});
l2.添加(新的int[]{20,20});
l3=新的ArrayList();
l3.add(新的int[]{100100});
l3.add(新int[]{200200});
l3.add(新的int[]{300300});
}
私有静态void permuteAndPrintValuestThreeLists_Iterative(){
对于(int i=0;i
到目前为止,我知道我需要一个包含3个列表的列表(在我的例子中,我添加了一个HashMap)。我也有这个解决方法,部分解决了这个问题

private static Map<Integer, List<int[]>> allLists = new HashMap<>();
private static void permuteAndPrintValuesThreeLists_Recursion(List<int[]> resultList, int mapIndex) {
    if (mapIndex == allLists.size()) {
        // Debug code
        for (int[] arr : resultList)
            for (int i = 0; i < arr.length; i++)
                System.out.println(arr[i] + " ");
        resultList.clear();
        System.out.println();
        return;
    }
    for (int i = 0; i < allLists.get(mapIndex).size(); i++) {
        int[] tmpArray = allLists.get(mapIndex).get(i);
        resultList.add(tmpArray);
        permuteAndPrintValuesThreeLists_Recursion(resultList, mapIndex + 1);
    }
}
private static Map allLists=new HashMap();
私有静态void permuteAndPrintValuestThrelists\u递归(列表结果列表,int-mapIndex){
如果(mapIndex==allLists.size()){
//调试代码
for(int[]arr:resultList)
对于(int i=0;i
通过添加一个try-catch块,我得到了我在问题中打印的输出。 我使用
set
为排列添加新值

现在,如果我想要第四个列表,这个方法仍然有效

private static void solution_withRecursion(List<int[]> resultList, int mapIndex) {
    if (mapIndex == allLists.size()) {
        printListValues(resultList);
        return;
    }
    for (int i = 0; i < allLists.get(mapIndex).size(); i++) {
        int[] tmpArray = allLists.get(mapIndex).get(i);
        try {
            resultList.set(mapIndex, tmpArray);
        } catch (IndexOutOfBoundsException e) {
            resultList.add(tmpArray);
        }
        solution_withRecursion(resultList, mapIndex + 1);
    }
}


private static void printListValues(List<int[]> list) {
    for (int[] arr : list) {
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
    System.out.println();
}
private static void solution\u带递归(List resultList,int-mapIndex){
如果(mapIndex==allLists.size()){
printListValues(结果列表);
返回;
}
for(int i=0;i
欢迎使用堆栈溢出!我们是一个问答网站,而不是一个编码出租服务。请解释到目前为止你尝试了什么以及为什么没有成功。这可能会给你一个想法,尽管这是一个寻找字符串“abc”的所有排列的广义问题。假设您的函数名为
perms
,它以字符串作为输入,以字符串列表作为输出。递归的基本情况是字符串长度为一个字符。对于给定的排列(索引为0..n-1的字符串),对于由排列返回的每个排列(索引为1..n-1的字符串),只返回一个仅包含一个字符的列表(续…),在索引为0、索引为1、索引为2的排列中插入字符串[0]。。在绳子的末端。返回此字符串列表,其长度为
n
阶乘长度。
private static void solution_withRecursion(List<int[]> resultList, int mapIndex) {
    if (mapIndex == allLists.size()) {
        printListValues(resultList);
        return;
    }
    for (int i = 0; i < allLists.get(mapIndex).size(); i++) {
        int[] tmpArray = allLists.get(mapIndex).get(i);
        try {
            resultList.set(mapIndex, tmpArray);
        } catch (IndexOutOfBoundsException e) {
            resultList.add(tmpArray);
        }
        solution_withRecursion(resultList, mapIndex + 1);
    }
}


private static void printListValues(List<int[]> list) {
    for (int[] arr : list) {
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
    System.out.println();
}