2017-02-19 72 views
0

我需要找到给定的n(用户输入)没有回溯的所有排列。Java设置/ setElementAt没有设置正确的值

我的尝试是:

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处理内存有一些奇怪的方法吗?或者这是什么原因?

预先感谢您!

回答

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

这是字面上设置在矢量permutations(i)同一对象作为permutations[i-1]。不同的价值 - 完全相同的对象。我认为这是你问题的根源。您需要复制矢量中的值。