2012-06-28 33 views
0

我有以下代碼排列。但它拋出了以下錯誤。r元素的排列從n

11111 
21111 
31111 
41111 
51111 
61111 
71111 
81111 
91111 
01111 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 
    at Permutation.main(nPr_3.java:22) 

該代碼是

HashSet<Integer[]> set = new HashSet<Integer[]>(); 
    int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; 
    int n = values.length; 
    int r = 5; 
    int i[] = new int[r]; 
    int rc = 0; 
    for(int j=0; j<Math.pow(n,r); j++) 
    { 
     Integer[] e = new Integer[r]; 
     while(rc<r) 
     { 
      e[rc] = values[i[rc]]; 
      System.out.print(values[i[rc]]); 
      rc++; 
     } 
     System.out.println(); 
     rc = 0; 
     set.add(e); 
     while(rc<r) 
     { 
      if(i[rc]<n) 
      { 
       i[rc]++; 
       break; 
      } 
      rc++; 
     } 
    } 

感謝。

+4

數組中只有'10'元素佔據索引'0 - 9'。沒有索引'10'。 –

回答

0

當引發異常時,i [rc](== i [0])在行[i] [rc] ++中增加,直到值爲10.但值僅包含帶有零度0..9的元素。