2012-06-28 3 views
0

순열을위한 다음 코드가 있습니다. 그러나 그것은 아래의 오류를 던지고있다.n 개의 r 요소 중 순열 변환

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

배열에 '0-9'라는 색인을 차지하는 '10'요소 만 있습니다. 인덱스 '10'이 없습니다. –

답변

0

i [rc] (== i [0])가 i [rc] ++에서 값 10까지 증가합니다. 그러나 값에는 0.ce가있는 요소 만 포함됩니다.