Java does not allow subindexing of arrays.
What you are referring to is trivially possible in C, but in Java you will need to either:
- copy the data to new arrays
- use a custom class abstracting from storage.
In java, there is no way that foo[0] permanently refers to the element bar[3] of another array.
If you want to work with int[][], you'll have to copy the arrays. Arrays.copyOfRange and System.arraycopy will be the most efficient choices, but at Sudoku size it does obviously not make much of a difference.
For the second approach, write a custom Matrix class. For example
class Matrix {
int[] flatStorage;
int[] offsets;
Matrix(int[] flatStorage, int[] offsets) {
this.flatStorage = flatStorage;
this.offsets = offsets;
}
void set(int x, int y, int val) {
flatStorage[ offsets[x] + y ] = val;
}
int get(int x, int y) {
return flatStorage[ offsets[x] + y ];
}
}
int[] sharedStorage = new int[27];
Arrays.fill(sharedStorage, -1); // Initialize with -1
int[] allOffsets = new int[]{0,9,18, 27,36,45, 54,63,72};
Matrix nineByNine = new Matrix(sharedStorage, allOffsets);
Matrix northEast = new Matrix(sharedStorage, new int[]{6,15,24});
Matrix southEast = new Matrix(sharedStorage, new int[]{60,69,78});
nineByNine.set(1,7, 2); // Write to middle of upper right quadrant
System.err.println(northEast.get(1, 1)); // Read - should be 2!
Add size information and similar things yourself.
board.= new int[3][3]for each of those. Currently you're just initializingse.sections