# 高手帮小弟我解读一下这个求幂集的程序，多谢

www.MyException.Cn  网友分享于：2013-12-11  浏览：49次

import java.util.*;

public class Collection {

public static void main(String[] args) {
Vector collection = new Vector();

Object[] o = function (collection);

for (int i = 0 ; i < o.length ; i++ ){
List a = (List)o[i];
if(a.size() > 0) {
System.out.println(a);
}
}
}

public static Object[] function(Vector collection) {

ArrayList pCollection = new ArrayList();
ArrayList collectionTemp = new ArrayList();

function1 (1, collection, collectionTemp, pCollection);

Object[] o = pCollection.toArray();

return o;

}

public static void function1(int i, Vector collection, ArrayList collectionTemp, ArrayList pCollection) {

if (i > collection.size()){
} else {

Object o = collection.elementAt(i-1);
function1(i+1,collection,collectionTemp,pCollection); //就这3条递归语句看不明白，我觉得关键是没有理解该算法的思想
collectionTemp.remove(collectionTemp.size()-1);
function1(i+1,collection,collectionTemp,pCollection);
}
}
}

------解决方案--------------------

------解决方案--------------------

Java code
```public static Object[] function(Vector collection) {

ArrayList pCollection = new ArrayList();
ArrayList collectionTemp = new ArrayList();

function1 (1, collection, collectionTemp, pCollection);

Object[] o = pCollection.toArray(); //Object数组的值来自于ArrayList pCollection，也就是说collectionTemp是过渡的

return o;

}
------解决方案--------------------用二进制数的原理自己写一个也很简单.
------解决方案--------------------呵呵，一开始我也看不明白，在本地运行了一下，结果是：[a, b, c][a, b][a, c][a][b, c][b][c]理解递归并不难，我觉得比较难想到的是处理后面的逻辑collectionTemp.remove(collectionTemp.size()-1);function1(i+1,collection,collectionTemp,pCollection);删除集合的最后一个元素再调用递归, 这个才是这个算法的关键.```