You should passedpass to your method the scope of the sub-matrix you want to copy, namely its starting and ending positions, both regarding the rows and columns, something like:
public Matrix submatrix(Matrix A, int row_begin, int row_end, int col_begin, int col_end) {
Matrix subM = new Matrix(row_end - row_begin, col_end - col_begin);
for (int r = row_begin, i=0; r < row_end; r++, i++) {
for (int c = col_begin, j= 0; c < col_end; c++, j++) {
subM.data[i][j] = A.data[r][c];
}
}
return subM;
}
Btw I am assuming a well behaved-behaved input. Normally, you would need to add additional checks on the variables row_begin, row_end, col_begin, col_end to ensure that you will not gotget into troubles (e.g., IndexOutofbounds).
Now if you really want to be able to skip rows and columns in the middle, then thinksthings get more complicated. One solution is to pass as a parameter also the range of columns/rows to be skipped, something like:
int [][] submatrix(data, 0 , 2, data.length, data.length, 0 , 3, 1, 2);
for (int[] ints : a) {
for (int anInt : ints)
System.out.print(anInt + " ");
System.out.println("\n");
}