fork(3) 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.  
  10. private static int defaultStep = 2;
  11. private static int defaultIndex = defaultStep;
  12.  
  13. public static void main (String[] args) throws java.lang.Exception {
  14. List<Integer> list= new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
  15. List<Integer> result = reformatList(list, defaultIndex);
  16.  
  17. System.out.println("result: " + result);
  18. }
  19.  
  20.  
  21. static List<Integer> reformatList(List<Integer> list, int idx) {
  22. //System.out.println("Size: " + list.size() + ", index: " + idx);
  23. int listSize = list.size();
  24. if (idx > listSize)
  25. return reformatList(list, idx - listSize);
  26.  
  27. List<Integer> newList = new ArrayList<>();
  28.  
  29. if (idx == 0) {
  30. newList.addAll(list.subList(idx + 1, listSize));
  31. }
  32.  
  33. if (idx != 0) {
  34. newList.addAll(list.subList(idx, listSize));
  35. newList.addAll(list.subList(0, idx - 1));
  36. }
  37.  
  38. int newListSize = newList.size();
  39. if (newListSize > 1) {
  40. idx = (newListSize < defaultStep) ? defaultStep - newListSize : defaultStep;
  41. return reformatList(newList, idx);
  42. }
  43.  
  44. return newList;
  45. }
  46. }
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
result: [3]