|
Not the one I wrote, the listAll method adds a parameter, when the prefix length passed in, it prints and ends the recursion
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);
}
} |
|