fork(8) 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 Random generator = new Random();
  11.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. double[][] mainArray = new double[7][8];
  15. for (int i = 0; i < mainArray.length; i++) {
  16. for(int j = 0; j < mainArray[i].length; j++) {
  17. mainArray[i][j] = generator.nextInt(255);
  18. }
  19. System.out.println(Arrays.toString(mainArray[i]));
  20. }
  21. System.out.println();
  22. System.out.println();
  23.  
  24. List<double[][]> l = chunks(mainArray, 2);
  25. for (double[][] chunk : l) {
  26. for (double[] row : chunk) {
  27. System.out.println(Arrays.toString(row));
  28. }
  29. System.out.println();
  30. }
  31. }
  32.  
  33. public static List<double[][]> chunks(double [][]larger, int chunksize) throws
  34. if (chunksize <= 0)
  35. throw new ArrayIndexOutOfBoundsException("Chunks must be atleast 1x1");
  36. int size = larger.length / chunksize * (larger[0].length / chunksize);
  37. List<double[][]> subArrays = new ArrayList<>();
  38.  
  39. for (int c = 0; c < size; c++) {
  40. double[][] sub = new double[chunksize][chunksize];
  41. int startx = (chunksize * (c / chunksize)) % larger.length;
  42. int starty = (chunksize * c) % larger[0].length;
  43.  
  44. if (starty + chunksize > larger[0].length) {
  45. starty = 0;
  46. }
  47. if (startx + chunksize > larger.length) {
  48. continue;
  49. }
  50.  
  51. for (int row = 0; row < chunksize; row++) {
  52. for (int col = 0; col < chunksize; col++) {
  53. sub[row][col] = larger[startx + row][col + starty];
  54. }
  55. }
  56. subArrays.add(sub);
  57. }
  58.  
  59. return subArrays;
  60. }
  61. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
[196.0, 179.0, 71.0, 25.0, 220.0, 177.0, 81.0, 42.0]
[28.0, 57.0, 13.0, 0.0, 144.0, 252.0, 200.0, 152.0]
[39.0, 102.0, 225.0, 141.0, 204.0, 90.0, 109.0, 63.0]
[2.0, 106.0, 219.0, 210.0, 141.0, 59.0, 228.0, 163.0]
[92.0, 1.0, 76.0, 119.0, 86.0, 34.0, 241.0, 183.0]
[147.0, 116.0, 101.0, 176.0, 153.0, 114.0, 3.0, 8.0]
[62.0, 87.0, 186.0, 73.0, 79.0, 165.0, 96.0, 15.0]


[196.0, 179.0]
[28.0, 57.0]

[71.0, 25.0]
[13.0, 0.0]

[204.0, 90.0]
[141.0, 59.0]

[109.0, 63.0]
[228.0, 163.0]

[92.0, 1.0]
[147.0, 116.0]

[76.0, 119.0]
[101.0, 176.0]

[28.0, 57.0]
[39.0, 102.0]

[13.0, 0.0]
[225.0, 141.0]

[141.0, 59.0]
[86.0, 34.0]

[228.0, 163.0]
[241.0, 183.0]