/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { { double[][] mainArray = new double[7][8]; for (int i = 0; i < mainArray.length; i++) { for(int j = 0; j < mainArray[i].length; j++) { mainArray[i][j] = generator.nextInt(255); } } List<double[][]> l = chunks(mainArray, 2); for (double[][] chunk : l) { for (double[] row : chunk) { } } } public static List<double[][]> chunks(double [][]larger, int chunksize) throws if (chunksize <= 0) int size = larger.length / chunksize * (larger[0].length / chunksize); List<double[][]> subArrays = new ArrayList<>(); for (int c = 0; c < size; c++) { double[][] sub = new double[chunksize][chunksize]; int startx = (chunksize * (c / chunksize)) % larger.length; int starty = (chunksize * c) % larger[0].length; if (starty + chunksize > larger[0].length) { starty = 0; } if (startx + chunksize > larger.length) { continue; } for (int row = 0; row < chunksize; row++) { for (int col = 0; col < chunksize; col++) { sub[row][col] = larger[startx + row][col + starty]; } } subArrays.add(sub); } return subArrays; } }
Standard input is empty
[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]