0
我想在呼叫递归方法,有结果了一套整数的finaly中检索: {10,20,30}但是在这个节目,我有porblem,递归方法
public static void main(String[] args) {
HashMap<Integer, Set<Integer>> myMap = new HashMap();
myMap.put(1, new HashSet(){{add(10);}});
myMap.put(2, new HashSet(){{add(20);}});myMap.get(2).add(30);
myMap.put(3, new HashSet());
HashSet<Integer> setInteg = new HashSet();
recursivFonc(setInteg, myMap, 1);
System.out.println(setInteg);
}
static HashSet recursivFonc(HashSet<Integer> setInteg, HashMap<Integer, Set<Integer>> map, int cont)
{
System.out.println(cont);
if(map.get(cont) != null)
{
Set<Integer> set = map.get(cont);
for(Integer intg : set)
{
setInteg.add(intg);
return recursivFonc(setInteg, map, cont);
}
}
return setInteg;
}
如何做我做到{10,20,30}最后一组?
实际上,由于StackOverflowError,它会非常快地崩溃。 – Viruzzo 2012-02-08 11:44:34
科斯的零钱!但为什么结果是[10,20]而不是[10,20,30]? 'return recursivFonc(setInteg,map,cont + 1);' – Mehdi 2012-02-08 12:00:22
@ user1149157 - 预期的结果应该是“[20,10,30]”?你是否尝试过增加'cont' **和**在循环外移动return语句。 – 2012-02-08 12:02:24