//列出一个数组中数的全部组合。如{1,2}则列出12,21 import java.util.*; public class ListCombinations1{ public static void main(String args[]){ String[] array = {"1","2","3"}; listCombinations(Arrays.asList(array),""); } public static void listCombinations(List candidate, String prefix){ if(candidate.isEmpty()){ System.out.println(prefix); } for(int i=0; i<candidate.size(); i++){ List temp = new LinkedList(candidate); listCombinations(temp, prefix+temp.remove(i)); } } }
//列出一个数组中数的全部组合。如{1,2}则列出1,2,12,21 import java.util.*; public class ListCombinations{ public static void main(String args[]){ String[] array = {"1","2","3"};//是String 而非int!!! listCombinations(Arrays.asList(array),""); } public static void listCombinations(List candidate, String prefix){ System.out.println(prefix);//与上段代码只有此处不同 for(int i=0; i<candidate.size(); i++){ List temp = new LinkedList(candidate); listCombinations(temp, prefix+temp.remove(i)); } } }