CCI 9.4 컬렉션의 모든 하위 세트
1257 단어 귀속
package cci;
import java.util.ArrayList;
public class CCI_9_4 {
//
public static ArrayList<ArrayList<Integer>> getSubsets(ArrayList<Integer> set){
if(set == null)
return null;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
//finish condition
if(set.size()==0){
//empty set
result.add(new ArrayList<Integer>());
return result;
}
int item = set.remove(set.size()-1);
ArrayList<ArrayList<Integer>> subsets = getSubsets(set);
result.addAll(subsets);
for(ArrayList<Integer> subset : subsets){
ArrayList<Integer> newSet = new ArrayList<Integer>(subset);
newSet.add(item);
result.add(newSet);
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> case1 = new ArrayList<Integer>();
for(int i=0; i<3; i++)
case1.add(i+1);
ArrayList<ArrayList<Integer>> result = getSubsets(case1);
for(ArrayList<Integer> subset : result){
for(Integer item : subset){
System.out.print(item + " ");
}
System.out.println();
}
}
}