简体   繁体   English

递归方法中int []的ArrayList

[英]ArrayList of int[] in a recursive method

I have a project to do for school in which i have to use a recursive method that will calculate all the switching possibilities between some numbers. 我有一个学校项目,必须使用一种递归方法来计算一些数字之间的所有切换可能性。 IE : [1,2] => 1,2 and 2,1. IE:[1,2] => 1,2和2,1。

So i've used this method and it seemed to be working properly when i just print the solutions on the console, but when i want to stock the tabs in an ArrayList (i need to use them later) it will always add the same order. 所以我使用了这种方法,当我只在控制台上打印解决方案时,它似乎可以正常工作,但是当我想将选项卡存储在ArrayList中时(我以后需要使用它们),它将总是添加相同的顺序。 In my exemple it would have added 1,2 and 1,2 instead of 1,2 and 2,1. 在我的例子中,它将增加1,2和1,2,而不是1,2和2,1。

Here's my code : 这是我的代码:

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;

}

} }

I use start = 0, input = {1,2,3} and the ArrayList is empty before the method starts. 我使用start = 0,input = {1,2,3},并且在方法开始之前ArrayList为空。

Hope you can help, thanks ! 希望能对您有所帮助,谢谢!

The problem is that you are adding a reference to an array into your ArrayList, reference that you then keep changing in your algorithm. 问题是您要在ArrayList中添加对数组的引用,然后在您的算法中不断更改引用。

By the end of it, you will have perm(N) copies of the same array. 到最后,您将拥有同一阵列的perm(N)个副本。

All you have to do is to add a deep-copy of the array into the ArrayList, like this: 您要做的就是将数组的深层副本添加到ArrayList中,如下所示:

al.add(Arrays.copyOf(input, input.length));

instead of 代替

al.add(input);

Printing the resulting ArrayList will then produce the following output: 打印结果ArrayList将产生以下输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM