2016-01-28 24 views
1

我得到我的代碼工作在預期的方式,但是當我嘗試使多個列表的排列它給了我第一個,並沒有改變它。 我需要使用Arraylist製作從1到10的排列列表,並捕獲代碼中可能出現的一個錯誤。然後,我們必須複製該代碼,以便獲得9種不同的排列行。我的隨機陣列給了我相同的答案,當它應該是不同的

import java.util.ArrayList; 
import java.util.Random; 
public class Permutations 
{ 

    ArrayList <Integer> Perm = new ArrayList <Integer>(); 
    ArrayList <Integer> Print = new ArrayList <Integer>(); 
    int counter = 9; 
    int List = 1; 
    public void ArrayList() 
    { 
     Perm.add(1); 
     Perm.add(2); 
     Perm.add(3); 
     Perm.add(4); 
     Perm.add(5); 
     Perm.add(6); 
     Perm.add(7); 
     Perm.add(8); 
     Perm.add(9); 
     Perm.add(10); 
    } 

    public void PermutationGenerator() 
    { 
     ArrayList(); 
     Random rand = new Random(); 
     int value = rand.nextInt(10) + 1; 
     while(Print.size() < 10) 
     { 
      try 
      { 
       while(Print.contains(value)) 
       { 
        value = rand.nextInt(10) + 1; 
       } 
       Print.add(value); 
       Perm.remove(value); 
      } 
      catch(IndexOutOfBoundsException a) 
      { 
       System.out.println(""); 
      } 
     } 
     System.out.println("List" + List + ":" + Print.toString()); 
     List++; 
     if(List < 10) 
     { 
      PermutationGenerator(); 
     } 
    } 

這就是打印出來:

List1:[9,6,5,4,10,2,8,7,1,3]  
List2:[9,6,5,4,10,2,8,7,1,3]  
List3:[9,6,5,4,10,2,8,7,1,3]  
List4:[9,6,5,4,10,2,8,7,1,3]  
List5:[9,6,5,4,10,2,8,7,1,3]  
List6:[9,6,5,4,10,2,8,7,1,3]  
List7:[9,6,5,4,10,2,8,7,1,3]  
List8:[9,6,5,4,10,2,8,7,1,3]  
List9:[9,6,5,4,10,2,8,7,1,3] 
+2

我認爲一旦你的打印尺寸爲10則不再執行while循環,並在打印出的同一個及以上 – JRowan

+0

那麼,我需要將所有值重置爲初始值吧去工作 –

+0

你爲什麼認爲它應該打印不同的值?你能描述一下我們哪個部分負責嗎? – Pshemo

回答

1

在PermutationGenerator結束,你CALLE遞歸的方法相同。在這一點上,變量Print裏面已經有10個隨機元素。

每次執行PermutationGenerator方法時,都需要清空Print變量,以便執行while循環。在你的情況下,Print變量總是有相同的10個元素,所以while循環只在第一次執行,並且Print變量永遠不會被修改。

+0

如果我刪除所有(列表) if(List <10) { PermutationGenerator(); } –

+0

我認爲它必須工作 –

+0

另一個提示:最好是嘗試迭代解決方案,並避免遞歸。 –

-1

我得到了答案,如果我將此行添加到最終的while循環中。

if(List < 10) 
    { 
     Print.removeAll(Print); 
     PermutationGenerator(); 
    } 
0
import java.util.ArrayList; 
import java.util.Random; 

public class Permutations { 

    ArrayList<Integer> Perm = new ArrayList<Integer>(); 
    ArrayList<Integer> Print = new ArrayList<Integer>(); 
    int counter = 9; 
    int List = 1; 

    public void ArrayList() { 
     Perm.add(1); 
     Perm.add(2); 
     Perm.add(3); 
     Perm.add(4); 
     Perm.add(5); 
     Perm.add(6); 
     Perm.add(7); 
     Perm.add(8); 
     Perm.add(9); 
     Perm.add(10); 
    } 

    public void PermutationGenerator() { 
     ArrayList(); 
     Random rand = new Random(); 
     int value = rand.nextInt(10) + 1; 
     //list: 1-9 
     while (Print.size() < 10) { 
      try { 
       while (Print.contains(value)) { 
        value = rand.nextInt(10) + 1; 
       } 
       Print.add(value); 
       Perm.remove(value); 
      } catch (IndexOutOfBoundsException a) { 
       System.out.println(""); 
      } 
     } 
     System.out.println("List" + List + ":" + Print.toString()); 
     List++; 
     if (List < 10) { 
      Print.removeAll(Print); 
      PermutationGenerator(); 
     } 
    } 
    public static void main(String[] args) { 
     Permutations permutations = new Permutations(); 
     permutations.PermutationGenerator(); 
    } 
} 
相關問題