Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归方法中int[]的ArrayList_Java_Loops_Recursion_Arraylist - Fatal编程技术网

Java 递归方法中int[]的ArrayList

Java 递归方法中int[]的ArrayList,java,loops,recursion,arraylist,Java,Loops,Recursion,Arraylist,我要为学校做一个项目,在这个项目中,我必须使用递归方法来计算一些数字之间的所有转换可能性。 即:[1,2]=>1,2和2,1 所以我使用了这种方法,当我在控制台上打印解决方案时,它似乎工作正常,但是当我想在ArrayList中存储选项卡时(我以后需要使用它们),它将始终添加相同的顺序。在我的示例中,它将添加1,2和1,2,而不是1,2和2,1 这是我的密码: public static void permute(int start, int[] input, ArrayList <int

我要为学校做一个项目,在这个项目中,我必须使用递归方法来计算一些数字之间的所有转换可能性。 即:[1,2]=>1,2和2,1

所以我使用了这种方法,当我在控制台上打印解决方案时,它似乎工作正常,但是当我想在ArrayList中存储选项卡时(我以后需要使用它们),它将始终添加相同的顺序。在我的示例中,它将添加1,2和1,2,而不是1,2和2,1

这是我的密码:

public  static void permute(int start, int[] input, ArrayList <int[]> al) { 
    //This method is recursive, it will open multiple instances of the input tab by calling itself and modify them, then stock tab in ArrayList when the operations are done for this tab.
    //ArrayList must be empty.

    //Printing tab if iterations for that specific tab are done
    if (start == input.length) {
        al.add(input);
        ////////////////////////////////
        // For printing tabs in console.
        // for(int x: input){
        // System.out.print(x);
        // }
        // System.out.println("");
        ////////////////////////////////
    //End the specific tab loop when it's printed 

    return;
    }
    for (int i = start; i < input.length; i++) {
        // Changing numbers
        int temp = input[i];
        input[i] = input[start];
        input[start] = temp;

        //////////////////////////////////////////////////
        // Tests to see algorithm steps
        //
        // System.out.print("temp : " + temp + " ... ");
        // System.out.print("i : "+i + " ... ");
        // System.out.print("start : " + start);
        // System.out.println("");
        // System.out.print("---");
        // for(int x: input){
        //  System.out.print(x);
        // }
        // System.out.println("");
        //////////////////////////////////////////////////

        //Changing numbers
        permute(start + 1, input, al);

       // Changing numbers
        int temp2 = input[i];
        input[i] = input[start];
        input[start] = temp2;

}
publicstaticvoidpermute(int-start,int[]输入,arraylistal){
//此方法是递归的,它将通过调用自身打开输入选项卡的多个实例并对其进行修改,然后在对该选项卡执行操作时将该选项卡保存在ArrayList中。
//ArrayList必须为空。
//如果特定选项卡的迭代已完成,则打印该选项卡
if(start==input.length){
al.添加(输入);
////////////////////////////////
//用于在控制台中打印选项卡。
//for(int x:输入){
//系统输出打印(x);
// }
//System.out.println(“”);
////////////////////////////////
//打印时结束特定选项卡循环
返回;
}
for(int i=start;i
}

我使用start=0,input={1,2,3},并且ArrayList在方法启动之前是空的


希望你能帮忙,谢谢

问题在于,您正在将对数组的引用添加到ArrayList中,然后在算法中不断更改该引用

最后,您将拥有同一数组的perm(N)个拷贝

您只需将数组的深度副本添加到ArrayList中,如下所示:

al.add(Arrays.copyOf(input, input.length));
而不是

al.add(input);
打印生成的ArrayList将产生以下输出:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

谢谢,伙计,成功了!但是我能问一下我的台词和深度拷贝的区别吗?我的意思是什么时候应该在输入引用上使用深度复制?您希望在任何时候都使用深度复制来获取对象的真正新副本,而不仅仅是对象当前引用的副本。后者不会保护您免受其他代码通过其持有的引用修改底层对象的影响,这正是让您感到困惑的地方。为了理解为什么这是必要的,谷歌为“java是按引用传递还是按值传递?”