Java set / setElementAt没有设置正确的值

时间:2017-02-19 00:25:46

标签: java algorithm vector combinatorics

我需要找到给定n(用户输入)没有回溯的所有排列。

我尝试的是:

import java.util.Scanner;
import java.util.Vector;

class Main {

    private static int n;
    private static Vector<Vector<Integer>> permutations = new Vector<>();



    private static void get_n() {

        Scanner user = new Scanner(System.in);

        System.out.print("n = ");

        n = user.nextInt();
    }
    private static void display(Vector<Vector<Integer>> permutations) {

        for (int i = 0; i < factorial(n) - 1; ++i) {
            for (int j = 0; j < n; ++j) {
                System.out.print(permutations.elementAt(i).elementAt(j) + " ");
            }
            System.out.println();
        }
    }



    private static int factorial(int n) {

        int result = 1;

        for (int i = 1; i <= n; ++i) {

            result *= i;
        }

        return result;
    }
    private static int max(Vector<Integer> permutation) {

        int max = permutation.elementAt(0);

        for (int i = 1; i < permutation.size(); ++i)
            if (permutation.elementAt(i) > max)
                max = permutation.elementAt(i);

        return max;
    }



    //  CHECKS FOR ELEMENT COUNT AND 0 - (n-1) APPARITION
    public static int validate_permutation(Vector<Integer> permutation) {

        // GOOD NUMBER OF ELEMENTS
        if (max(permutation) != permutation.size() - 1)
            return 0;

        // PROPER ELEMENTS APPEAR
        for (int i = 0; i < permutation.size(); ++i)
            if (!permutation.contains(i))
                return 0;

        return 1;
    }

    private static Vector<Integer> next_permutation(Vector<Integer> permutation) {

        int i;

        do {
            i = 1;

            // INCREMENT LAST ELEMENT
            permutation.set(permutation.size() - i, permutation.elementAt(permutation.size() - i) + 1);

            // IN A P(n-1) PERMUTATION FOUND n. "OVERFLOW"
            while (permutation.elementAt(permutation.size() - i) == permutation.size()) {

                // RESET CURRENT POSITION
                permutation.set(permutation.size() - i, 0);

                // INCREMENT THE NEXT ONE
                ++i;
                permutation.set(permutation.size() - i, permutation.elementAt(permutation.size() - i) + 1);
            }
        } while (validate_permutation(permutation) == 0);


        // OUTPUT
        System.out.print("output of next_permutation:\t\t");
        for (int j = 0; j < permutation.size(); ++j)
            System.out.print(permutation.elementAt(j) + " ");
        System.out.println();

        return permutation;
    }

    private static Vector<Vector<Integer>> permutations_of(int n) {

        Vector<Vector<Integer>> permutations = new Vector<>();

        // INITIALIZE PERMUTATION SET WITH 0
        for (int i = 0; i < factorial(n); ++i) {

            permutations.addElement(new Vector<>());

            for(int j = 0; j < n; ++j)
                permutations.elementAt(i).addElement(0);
        }


        for (int i = 0; i < n; ++i)
            permutations.elementAt(0).set(i, i);

        for (int i = 1; i < factorial(n); ++i) {

            // ADD THE NEXT PERMUTATION TO THE SET
            permutations.setElementAt(next_permutation(permutations.elementAt(i - 1)), i);

            System.out.print("values set by permutations_of:\t");
            for (int j = 0; j < permutations.elementAt(i).size(); ++j)
                System.out.print(permutations.elementAt(i).elementAt(j)  + " ");
            System.out.println("\n");
        }

        System.out.print("\nFinal output of permutations_of:\n\n");
        display(permutations);

        return permutations;
    }



    public static void main(String[] args) {

        get_n();

        permutations.addAll(permutations_of(n));
    }
}

现在,运行代码时问题很明显。 next_permutation在调用时输出正确的排列,将值正确设置为相应的排列向量,但最终结果是最后排列的大量副本,这使我相信每次next_permutation输出新的排列时设置为排列向量,以某种方式,排列也被复制到其他排列的所有上。我无法弄清楚为什么我的生活。

我尝试了set,​​setElementAt和一个我没有初始化置换向量拳头的实现,但添加了next_permutation用add()输出的排列,我遇到了完全相同的问题。 Java处理内存有一些奇怪的方式吗?或者这是什么原因?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

permutations.setElementAt(next_permutation(permutations.elementAt(i - 1)), i);

这实际上是将permutations(i)处的向量设置为permutations[i-1]相同的对象。不是相同的值 - 完全相同的对象。我认为这是你问题的根源。您需要复制向量中的值。