62,614
社区成员
发帖
与我相关
我的任务
分享
/**
* xxx 20181018
* 从整形ArrayList source中 抽取n个元素,所构成的组合为 ArrayList<ArrayList<Integer>> result
* @param ArrayList<Integer> source
* @param int n
* @return ArrayList<ArrayList<Integer>> result
*/
public static ArrayList<ArrayList<Integer>> CombinationT(ArrayList<Integer> source,int n)
{ System.out.println("CombinationT(source,n):source.size()="+source.size()+"; n="+n);
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
if(n==1)
{
for(int i=0;i<source.size();i++)
{ Object t=source.get(i);
ArrayList a= new ArrayList();
a.add(t);
result.add(a);
}
}
else if(source.size()==n)
{
result.add(source);
}
else
{
ArrayList<Integer> psource=new ArrayList<Integer>();
//[source.size()-1];
for(int i=0;i<source.size()-1;i++)
{
psource.add(source.get(i));
}
//报错信息所提示的84行代码
result.addAll(CombinationT(psource,n));
ArrayList<ArrayList<Integer>> tmp=CombinationT(psource,n-1);
for(int i=0;i<tmp.size();i++)
{
ArrayList<Integer> rs=new ArrayList<Integer>();
for(int j=0;j<n-1;j++)
{
rs.add(tmp.get(i).get(j));
}
rs.add(source.get(source.size()-1));
result.add(rs);
}
}
return result;
}