public static void main(String[] args) {
List list =new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(10);
list.add(11);
list.add(12);
int num =8;
getList(list,0,new int[num],0,num);
System.out.println("times:"+times);
System.out.println("count:"+count);
getList1(list,0,new int[num],0,num);
System.out.println("times1:"+times1);
System.out.println("count1:"+count1);
}
/**
* 获取集合中,指定个数 不重复的组合
* @param list
* @param start
* @param result
* @param index
* @param NUM
*/
static int times =0;
static int count =0;
static int times1 =0;
static int count1 =0;
/**
* 从集合中,获取指定个数 不带重复的组合
* @param list
* @param start
* @param result
* @param index
* @param NUM
*/
public static void getList(List list, int start,int[] result,int index,final int NUM){
times++;
if(index +1 == NUM){
for (int i = start; i < list.size(); i++) {
result[index] = list.get(i);
System.out.println("-------"+ArrayUtils.toString(result));
count++;
}
}else {
for (int i = start; i < list.size(); i++) {
// 如果后面的集合凑不齐NUM 直接break
if(list.size() - i < NUM - index ){
break;
}
result[index] = list.get(i);
getList(list,i+1,result,index+1,NUM);
}
}
}
public static void getList1(List list, int start,int[] result,int index,final int NUM){
times1++;
if(index +1 == NUM){
for (int i = start; i < list.size(); i++) {
result[index] = list.get(i);
System.out.println(ArrayUtils.toString(result));
count1++;
}
}else {
for (int i = start; i < list.size(); i++) {
result[index] = list.get(i);
getList1(list,i+1,result,index+1,NUM);
}
}
}