加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java全排列通用工具类

发布时间:2020-12-15 03:13:03 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 全排列处理接口???? public interface PermutationProcessorT { void process(T[]array);} 全排列类???? public final class FullPermutation { publi

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

全排列处理接口????

public interface PermutationProcessor<T> {
    void process(T[]array);
}

全排列类????

public final class FullPermutation {

    public  static <T> void permutate(T a[],PermutationProcessor<T> processor) {
        permutate(a,a.length,processor);

    }
   static <T>  void permutate(T a[],int m,int n,PermutationProcessor<T> processor) {
        int i;
        T t;
        if (m < n - 1) {
            permutate(a,m + 1,n,processor);
            for (i = m + 1; i < n; i++) {
                swap(a,m,i);
                permutate(a,processor);
                swap(a,i);
            }
        } else {
            processor.process(a);
        }
    }

    private static <T> void swap(T[] a,int i) {
        T t;
        t = a[m];
        a[m] = a[i];
        a[i] = t;
    }
}

[代码]调用示例????

    public static void main(String[] args) {
        Integer[] a={1,2,4};
        FullPermutation.permutate(a,new PermutationProcessor<Integer>() {
            @Override
            public void process(Integer[] array) {
                for(int i:array){
                    System.out.printf("%d ",i);
                }
                System.out.println();
            }
        });
    }

[代码]运行结果????

1 2 4 
1 4 2 
2 1 4 
2 4 1 
4 2 1 
4 1 2 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读