循环递归

题目:把一个数组里的数组合全部列出,比如1 2 列出来为1, 2, 12, 21

代码:

 1 import java.util.*;
 2 import java.io.*;
 3 public class Test {
 4     
 5     public static void main(String[] args) throws Exception{
 6         String[] array = new String[]{
 7                 "1","2","3"
 8         };
 9         listAll(Arrays.asList(array), "");
10     }
11     public static void listAll(List candidate, String prefix){
12         System.out.println(prefix);
13         
14         for (int i = 0; i < candidate.size(); i++) {
15             List temp = new LinkedList(candidate);
16             listAll(temp, prefix+temp.remove(i));
17         }
18     }
19 }

方案分析:

此处listAll中temp参数有误,应该是temp.remove(i)之后的temp。

 

posted @ 2013-12-31 22:06  soul390  阅读(140)  评论(0编辑  收藏  举报