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?
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.
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.
精彩评论