Guest User

Untitled

a guest
May 20th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. package playground;
  2.  
  3. public class Playground {
  4.  
  5. public static void getAllPermutations(Object[] array) throws IllegalArgumentException {
  6. if ((array == null) || (array.length == 0)) {
  7. throw new IllegalArgumentException("Not defined!");
  8. } else {
  9. getAllPermutations(array, array.length);
  10. }
  11. }
  12.  
  13. public static void getAllPermutations(Object[] array, int n) {
  14. if (n == 1) {
  15. for (int i = 0; i < array.length; i++) {
  16. System.out.print(array[i]);
  17. }
  18. } else {
  19. for (int i = 0; i < n; i++) {
  20. Object temp = array[n - 1];
  21. array[n - 1] = array[i];
  22. array[i] = temp;
  23. getAllPermutations(array, n - 1);
  24. temp = array[n - 1];
  25. array[n - 1] = array[i];
  26. array[i] = temp;
  27. }
  28. }
  29. }
  30.  
  31. public static void main(String[] args) {
  32. Object[] test = new Object[3];
  33. for (int i = 0; i < test.length; i++) {
  34. test[i] = i;
  35. }
  36. getAllPermutations(test);
  37. }
  38. }
Add Comment
Please, Sign In to add comment