fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9. private static int defaultStep = 2;
  10. private static int defaultIndex = defaultStep;
  11.  
  12. public static void main (String[] args) throws java.lang.Exception {
  13. List<Integer> list= new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
  14.  
  15. List<Integer> result = reformatList(list, defaultIndex);
  16. System.out.println("result: " + result);
  17. }
  18.  
  19. private static List<Integer> reformatList(List<Integer> list, int idx) {
  20. if (list.size() == 1) {
  21. return list;
  22. }
  23.  
  24. if (idx > list.size())
  25. return reformatList(list, idx - list.size());
  26.  
  27. System.out.print(list.get(idx - 1) + ", ");
  28. list.remove(idx - 1);
  29.  
  30. return reformatList(list, defaultStep + idx - 1);
  31. }
  32. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
2, 4, 1, 5, result: [3]