public class AllCombination
{
private static void showAllCombination(Object[] objs, int lastIndex)
{
if(lastIndex < 0)
{
for(Object obj:objs)
{
System.out.print(obj);
}
System.out.println();
return;
}
Object temp = null;
for(int i = 0; i<=lastIndex; ++i)
{
temp = objs[i];
objs[i] = objs[lastIndex];
objs[lastIndex] = temp;
showAllCombination(objs, lastIndex - 1);
temp = objs[i];
objs[i] = objs[lastIndex];
objs[lastIndex] = temp;
}
}
public static void showAllCombination(Object[] objs)
{
showAllCombination(objs, objs.length - 1);
}
public static void main(String[] args)
{
Character[] array = {'1','2','3','4','5','6'};
showAllCombination(array);
}
}
我这个程序是输出六个字符的全排列情况,由于情况很多,建议把array改为4个来运行看看结果。
楼主的意思是要全排列呢?还是所有组合(可以不包含所有字符)?
|