组合

 1import java.util.*;
 2
 3public class Mains{
 4    public static List<List<Integer>> resultss = new ArrayList<>();
 5
 6    public void combinations(List<Integer> selected, List<Integer> data, int num){
 7        if(num == 0){
 8            resultss.add(new ArrayList<Integer>(selected));
 9            return;
10        }
11        if(data.size() == 0 ){
12            System.out.print("");
13            return;
14        }
15        selected.add(data.get(0));
16        combinations(selected, data.subList(1, data.size()), num -1);
17        selected.remove(data.get(0));
18        combinations(selected, data.subList(1, data.size()), num );
19    }
20    public static void main(String[] args) {
21        Mains combin = new Mains();
22        int[] nums = new int[]{1,2,3,4,5};
23        combin.combinations(new ArrayList<Integer>(),Arrays.asList(1,2,3,4),3);
24        for (List<Integer> res : Mains.resultss) {
25            System.out.println(res.toString());
26        }
27    }
28}
29
30/*
31Output
32
33[1, 2, 3]
34[1, 2, 4]
35[1, 3, 4]
36[2, 3, 4]
37
38*/