本文介绍了n个元素中的r个元素的排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下用于置换的代码.但这会引发以下错误.
I have the following code for permutation. But it is throwing the below error.
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++;
}
}
谢谢.
推荐答案
引发异常时,i [rc](== i [0])已在i [rc] ++行中递增,直到值10 .但是值仅包含索引为0..9的元素.
When the exception is thrown i[ rc ]( ==i[ 0 ] ) has been incremented in the line i[rc]++ until the value 10. But values contains only elements with indecees 0..9.
这篇关于n个元素中的r个元素的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!