|
发表于 2020-4-12 14:30:01
|
显示全部楼层
不就是我写的那个阿,listAll方法加一个参数,当传进来的prefix长度,就打印并结束递归
public static void main(String[] args) throws Exception {
String[] array = new String[] {
"1", "2", "3", "4"
};
listAll(Arrays.asList(array), "", 3);
}
public static void listAll(List candidate, String prefix, int length) {
// if (candidate == null || candidate.isEmpty()) {
if (prefix.length() == length) {
System.out.println(prefix);
}
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i), length);
}
} |
|