当前位置: 首页 > 知识库问答 >
问题:

计算数组的所有排列

楚建柏
2023-03-14

我正在尝试编写一种方法来将数组置换为所有可能的排列。我将每个数组以ArrayList的形式,翻转两个元素,然后将ArrayList返回到ArrayList of ArrayList。如果我在翻转两个元素后将每个数组打印到屏幕上,则按预期进行打印。[1,2,3]前两个元素翻转打印为[2,1,3],但当我将置换的ArrayList添加到另一个ArrayList时,它们都打印为[1,2,3]

代码:

public static void arrays() {
    
    //convert the input Lists to ArrayLists
    ArrayList<Integer> a = new ArrayList<Integer>();
    a.add(1);
    a.add(2);
    a.add(3);
    //ArrayList<Integer> b = new ArrayList<Integer>(B);
    
    //an array with 3 elements will have 1 * 2 * 3 permutations
    Integer numberOfPossiblePermutations = numberOfPermutations(a);
    
    ArrayList<ArrayList<Integer>> allPermutations = new ArrayList<ArrayList<Integer>>();
    allPermutations.add(a);
    
    System.out.println("The properly rearranged arrays");
    while (allPermutations.size() < numberOfPossiblePermutations) {
        //get the last array in the list of arrays
        ArrayList<Integer> lastArray = allPermutations.get(allPermutations.size()-1);
        ArrayList<Integer> arrayToAdd = lastArray;
        
        for (int i=0; i+1<lastArray.size(); i++) {
            //flip two elements in the array
            
            int x = lastArray.get(i);
            int y = lastArray.get(i+1);
            arrayToAdd.set(i+1, x);
            arrayToAdd.set(i, y);
            System.out.println(arrayToAdd);
            allPermutations.add(arrayToAdd);
        }
        
    }
    
    System.out.println();
    System.out.println("but when I add each ArrayList to another ArrayList they are coming out like this");
    
    for (int i=0; i<allPermutations.size(); i++) {
        System.out.println(allPermutations.get(i));
    }
    
}

//accepts an array and returns the possible number of permutations
public static Integer numberOfPermutations(ArrayList<Integer> a) {
    Integer numberOfPermutations = 1;
    for (int i=1; i<=a.size(); i++) {
        numberOfPermutations *= i;
    }
    return numberOfPermutations;    
}

输出:

>

  • 正确重新排列的数组

    [2, 1, 3]

    [2, 3, 1]

    [3, 2, 1]

    [3,1,2]

    [1,3,2]

    [1, 2, 3]

    但是,当我将每个ArrayList添加到另一个ArrayList时,结果是这样的

    [1, 2, 3]

    [1, 2, 3]

    [1, 2, 3]

    [1, 2, 3]

    [1, 2, 3]

    [1, 2, 3]

    [1, 2, 3]

  • 共有1个答案

    莫骞仕
    2023-03-14

    您只与单个ArrayList实例交互,但以不同的名称调用它。因此,当您打印它时,您在该对象生命周期的不同阶段打印它,但它仍然是同一个实例。

    这一行:

    ArrayList<Integer> arrayToAdd = lastArray;
    

    不复制对象;它只是给同一个对象一个不同的名称。您需要显式复制它。例如:

    ArrayList<Integer> arrayToAdd = new ArrayList<Integer>();
    arrayToAdd.addAll(lastArray);
    
     类似资料:
    • 问题内容: 我想找到一组整数的子集。这是具有回溯功能的“子集总和”算法的第一步。我已经编写了以下代码,但是没有返回正确的答案: 例如,如果我要计算set = {1,3,5}的子集,则我的方法的结果是: 我希望它产生: 我认为问题出在零件list.removeAll(list);中。但我不知道如何纠正它。 问题答案: 你想要的就是Powerset。这是一个简单的实现: 我将为你提供一个示例,说明该算

    • 北卡罗来纳州彩票提供了几场平局游戏,其中两场是选3和选4。在0和9(含9)之间分别选择3或4位数字,数字可以重复(例如,9-9-9是有效的组合)。在这个例子中,我将使用Pick3,因为它更容易使用,但我试图使它成为一个通用的解决方案,可以使用任何数量的数字。 选3和选4的一个特点是“1选1”,这意味着如果至少有一个号码比你的票上的号码高1或低1,你就赢了一个奖。

    • 问题内容: 我想计算所有奇数数组索引的总和,但是在寻找正确的方法时遇到了一些麻烦。 到目前为止,这是我的代码: 关于为何不起作用的任何想法,或者更简单的方法?为了澄清,我想在奇数数组索引位置添加所有数字,所以。 编辑: 忘记提及我只想添加1、3、5、7、9、11,而不是13。 问题答案: 刚刚编辑了代码:

    • 我有一个数字数组,现在我必须通过生成给定数组的所有可能子数组并应用一些条件来找到元素之和。 条件是,对于每个子阵列,获取最小值,并找到其中的元素总数,然后将两者相乘(最小值*总数)。最后,将所有子阵列的所有这些相乘值相加。 以下是问题陈述: 使用下面的公式找到所有可能的子数组的总和: 和(左,右)=(最小的arr[i]) * (∑ arr[i]),其中i的范围从左到右。 例子: 子数组是:[sta

    • 我正在尝试构造一个程序,该程序将获取一个int({1,2,3})数组和一个长度值,并计算该数组的所有可能组合。 例如: 这将输出: 但是当我尝试在 for 循环中调用可能的梳子时,我不断收到堆栈溢出错误 }

    • 问题内容: 给定一个PHP字符串数组,例如: 如何生成此数组元素的所有可能排列?即: 问题答案: function pc_permute($items, $perms = array()) { if (empty($items)) { echo join(‘ ‘, $perms) . “ “; } else { for ($i = count($items) - 1; $i >= 0; –$i)