java 全排列

有一个整数 0<n<10 输出n的阶乘项 如 当n = 3 时,如 123 132 231 213 312 321 就是输出n的阶乘项 当n = 4 的时候组成的是四位数 , 5的时候是5位数,其中每位上都是不同的。。不重复 当给定一个n 输出所有项
写一个java方法用来实现上述功能

我想要最简单的一种实现的方法,易理解(最好数组能实现)。现在已经实现了,寻求新的方法。

[code="java"]
/**

  • 输出全排列所有项
  • @author Administrator
    /
    public class GroupSort {
    /
    *

    • 0~10之间排列组合所需数组 */ private static Integer[] base = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    /**

    • 根据给定大小创建标识数组
    • @param len */ public static void printAll(Integer len) { base = new Integer[len]; for (int i = 0; i < len; i++) { base[i] = (i + 1); } check(base, 0, base.length-1); }

    /**

    • 排列组合
    • @param base
    • @param start
    • @param end */ public static void check(Integer[] base, int start, int end) { if (start == end) { System.out.print("["); for (int i = 0; i <= end; i++) { if(i==end){ System.out.print(base[i]); }else{ System.out.print(base[i]+","); } } System.out.println("]"); } else { for (int i = start; i <= end; i++) { Integer temp = base[start]; base[start] = base[i]; base[i] = temp; check(base, start + 1, end); temp = base[start]; base[start] = base[i]; base[i] = temp; } } }

    /**

    • 测试
    • @param args */ public static void main(String[] args) { //计算量过大,建议测试数据不要超过5 //printAll(new java.util.Random().nextInt(10)); printAll(3); } } [/code]