Try this piece of code, it can display different indices inside a given block, obtained using the int block = (((row / 3) * 3) + (column / 3)); function block = (((row / 3) * 3) + (column / 3));
import java.util.*; public class TwoDArray { public static void main(String... args) throws Exception { Scanner scanner = new Scanner(System.in); int[][] array = { {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, 31, 32, 33, 34, 35}, {36, 37, 38, 39, 40, 41, 42, 43, 44}, {45, 46, 47, 48, 49, 50, 51, 52, 53}, {54, 55, 56, 57, 58, 59, 60, 61, 62}, {63, 64, 65, 66, 67, 68, 69, 70, 71}, {72, 73, 74, 75, 76, 77, 78, 79, 80} }; displayMatrix(array); displayEachBlock(array); } private static void displayMatrix(int[][] array) { for (int i = 0; i < array.length; i++) { if (i == 3 || i == 6) System.out.println("------------------------------------"); for (int j = 0; j < array[i].length; j++) { System.out.format("%-3s", array[i][j]); if (j == 2 || j == 5 || j == 8) System.out.print(" | "); } System.out.println(); } System.out.println("------------------------------------"); } private static void displayEachBlock(int[][] array) { for (int i = 0; i < array.length; i += 3) { for (int j = 0; j < array[i].length; j += 3) { int block = (((i / 3) * 3) + (j / 3)); System.out.println("Block : " + block); int[][] newArray = new int[3][3]; int newRow = 0; for (int k = i; k < (i + 3); k++) { int newColumn = 0; for (int l = j; l < (j + 3); l++) {
And here is the result of this piece of code:
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 31 32 | 33 34 35 | 36 37 38 | 39 40 41 | 42 43 44 | 45 46 47 | 48 49 50 | 51 52 53 | ------------------------------------ 54 55 56 | 57 58 59 | 60 61 62 | 63 64 65 | 66 67 68 | 69 70 71 | 72 73 74 | 75 76 77 | 78 79 80 | ------------------------------------ Block : 0 [0][0] : 0 [0][1] : 1 [0][2] : 2 [1][0] : 9 [1][1] : 10 [1][2] : 11 [2][0] : 18 [2][1] : 19 [2][2] : 20 Block : 1 [0][0] : 3 [0][1] : 4 [0][2] : 5 [1][0] : 12 [1][1] : 13 [1][2] : 14 [2][0] : 21 [2][1] : 22 [2][2] : 23 Block : 2 [0][0] : 6 [0][1] : 7 [0][2] : 8 [1][0] : 15 [1][1] : 16 [1][2] : 17 [2][0] : 24 [2][1] : 25 [2][2] : 26 Block : 3 [0][0] : 27 [0][1] : 28 [0][2] : 29 [1][0] : 36 [1][1] : 37 [1][2] : 38 [2][0] : 45 [2][1] : 46 [2][2] : 47 Block : 4 [0][0] : 30 [0][1] : 31 [0][2] : 32 [1][0] : 39 [1][1] : 40 [1][2] : 41 [2][0] : 48 [2][1] : 49 [2][2] : 50 Block : 5 [0][0] : 33 [0][1] : 34 [0][2] : 35 [1][0] : 42 [1][1] : 43 [1][2] : 44 [2][0] : 51 [2][1] : 52 [2][2] : 53 Block : 6 [0][0] : 54 [0][1] : 55 [0][2] : 56 [1][0] : 63 [1][1] : 64 [1][2] : 65 [2][0] : 72 [2][1] : 73 [2][2] : 74 Block : 7 [0][0] : 57 [0][1] : 58 [0][2] : 59 [1][0] : 66 [1][1] : 67 [1][2] : 68 [2][0] : 75 [2][1] : 76 [2][2] : 77 Block : 8 [0][0] : 60 [0][1] : 61 [0][2] : 62 [1][0] : 69 [1][1] : 70 [1][2] : 71 [2][0] : 78 [2][1] : 79 [2][2] : 80