问题
Possible Duplicate:
Generating all permutations of a given string
I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?
回答1:
Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.
回答2:
It may or may not be optimal as far as performance goes, but if you're looking for a way to do it with writing relatively little code and having it be clear and maintainable, you want a recursive method rather than nested loops.
来源:https://stackoverflow.com/questions/5876716/generate-all-permutations-in-java