把一个数组里的数组合全部列出了 比如1 2 列出来为 1, 2 ,12,21,
代码是:
public static void main(String[] args) throws Exception {
String[] array = new String[] {
"1", "2", "3", "4"
};
listAll(Arrays.asList(array), "");
}
public static void listAll(List candidate, String prefix) {
// if (candidate.isEmpty()) {
System.out.println(prefix);
// }
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i));
}
}
=====输出=====
1
12
123
1234
124
1243
13
132
1324
134
1342
14
142
1423
143
1432
2
21
213
2134
214
2143
23
231
2314
234
2341
24
241
2413
243
2431
3
31
312
3124
314
3142
32
321
3214
324
3241
34
341
3412
342
3421
4
41
412
4123
413
4132
42
421
4213
423
4231
43
431
4312
432
4321 请高人指点一下:从1234变为123到12 再到124时怎么转变的啊 ?多谢了
代码是:
public static void main(String[] args) throws Exception {
String[] array = new String[] {
"1", "2", "3", "4"
};
listAll(Arrays.asList(array), "");
}
public static void listAll(List candidate, String prefix) {
// if (candidate.isEmpty()) {
System.out.println(prefix);
// }
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i));
}
}
=====输出=====
1
12
123
1234
124
1243
13
132
1324
134
1342
14
142
1423
143
1432
2
21
213
2134
214
2143
23
231
2314
234
2341
24
241
2413
243
2431
3
31
312
3124
314
3142
32
321
3214
324
3241
34
341
3412
342
3421
4
41
412
4123
413
4132
42
421
4213
423
4231
43
431
4312
432
4321 请高人指点一下:从1234变为123到12 再到124时怎么转变的啊 ?多谢了
System.out.println(prefix);
虽然是递归函数,但是关注输出的prefix应该很容易看