Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package playground;
- public class Playground {
- public static void getAllPermutations(Object[] array) throws IllegalArgumentException {
- if ((array == null) || (array.length == 0)) {
- throw new IllegalArgumentException("Not defined!");
- } else {
- getAllPermutations(array, array.length);
- }
- }
- public static void getAllPermutations(Object[] array, int n) {
- if (n == 1) {
- for (int i = 0; i < array.length; i++) {
- System.out.print(array[i]);
- }
- } else {
- for (int i = 0; i < n; i++) {
- Object temp = array[n - 1];
- array[n - 1] = array[i];
- array[i] = temp;
- getAllPermutations(array, n - 1);
- temp = array[n - 1];
- array[n - 1] = array[i];
- array[i] = temp;
- }
- }
- }
- public static void main(String[] args) {
- Object[] test = new Object[3];
- for (int i = 0; i < test.length; i++) {
- test[i] = i;
- }
- getAllPermutations(test);
- }
- }
Add Comment
Please, Sign In to add comment