fork(14) download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static boolean nextPermutation(List<Integer> list) {
  5. int i = list.size() - 2;
  6. while (i >= 0 && list.get(i) >= list.get(i + 1))
  7. i--;
  8.  
  9. if (i < 0)
  10. return false;
  11.  
  12. int j = list.size() - 1;
  13. while (list.get(i) >= list.get(j))
  14. j--;
  15.  
  16. Collections.swap(list, i, j);
  17. Collections.reverse(list.subList(i + 1, list.size()));
  18. return true;
  19. }
  20.  
  21. public static void main(String[] args) {
  22. List<Integer> a = Arrays.asList(new Integer[] {1, 2, 3, 4});
  23. do {
  24. System.out.println(a);
  25. } while (nextPermutation(a));
  26. }
  27. }
  28.  
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 2, 3]
[1, 4, 3, 2]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 1, 3]
[2, 4, 3, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 1, 2, 3]
[4, 1, 3, 2]
[4, 2, 1, 3]
[4, 2, 3, 1]
[4, 3, 1, 2]
[4, 3, 2, 1]