fork 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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<String> initialCollection = new ArrayList<String>();
  13. for(int i = 0; i < 31; i++) {
  14. initialCollection.add("" + i);
  15. }
  16. List<List<String>> resultCollections = new ArrayList<>();
  17.  
  18. int remainder = initialCollection.size() % 5;
  19. int minimumCollectionSize = initialCollection.size() / 5;
  20.  
  21. for (int i = 0; i < 5; i++) {
  22. int startIndex = i * minimumCollectionSize;
  23. int endIndex = (i + 1) * minimumCollectionSize;
  24.  
  25. if (i < remainder) {
  26. startIndex += i;
  27. endIndex += i + 1;
  28. } else {
  29. startIndex += remainder;
  30. endIndex += remainder;
  31. }
  32. //System.out.println(startIndex + "/" + endIndex);
  33. List<String> collectionPart = new ArrayList<>(initialCollection.subList(startIndex, endIndex));
  34. System.out.println(collectionPart.size() + "/ " + collectionPart);
  35. resultCollections.add(collectionPart);
  36. }
  37.  
  38. System.out.println(resultCollections);
  39. }
  40. }
Success #stdin #stdout 0.17s 57912KB
stdin
Standard input is empty
stdout
7/ [0, 1, 2, 3, 4, 5, 6]
6/ [7, 8, 9, 10, 11, 12]
6/ [13, 14, 15, 16, 17, 18]
6/ [19, 20, 21, 22, 23, 24]
6/ [25, 26, 27, 28, 29, 30]
[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30]]