列出數(shù)組中元素組合的所有情況
比如:[1, 2, 3] --> 123, 132, 213, 231, 312, 321
代碼實(shí)現(xiàn):
public class TestQuestion {
public static void getAll(List<Integer> list, String prefix) {
for (int i = 0; i < list.size(); i++) {
List tem = new ArrayList();
tem.addAll(list);
String res = prefix + tem.get(i);
tem.remove(i);
if (tem.size() > 0) {
getAll(tem, res);
} else {
System.out.println(res);
}
}
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
//list.add(4);
getAll(list, "哈");
}
}
結(jié)果:
哈123
哈132
哈213
哈231
哈312
哈321