获取实现多个数据的全部排列组合

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrangedUtil {
    private static List<List<?>> allList = new ArrayList<>();
    public static <T>void getAll(List<T> list,List<T> list2,int n){
        for (T object : list2) {
            List<T> ll = new ArrayList<>();
            ll.add(object);
            List<T> ll2 = new ArrayList<>(list2);
            ll2.remove(object);
            ll.addAll(list);
            getAll(ll,ll2,n);
            if(ll.size() == n){
                allList.add(ll);
            }
        }
    }
    public static void main(String[] args) {
        List<Integer> data = new ArrayList<>();
        Collections.addAll(data, 1,2,3);
        getAll(new ArrayList<Integer>(),data,data.size());
        for (List<?> list: allList) {
            System.out.println(list);
        }
    }
}

运行结果:

 

posted on 2016-10-03 03:30  大别山人  阅读(1151)  评论(0编辑  收藏  举报