1 - Reviewing the code that you place, I find the first error is on the line
int[][] redef_colum = new int[m.length][m.length + 1];
This is why you are defining the redef_colum
matrix with only the number of rows; this line should be
int[][] redef_colum = new int[m.length][m[0].length + 1];
2 - This part of the copying cycle is not necessary
if (c < redef_colum.length) {
redef_colum[f][c] = m[f][c];
} else {
redef_colum[f][c] = 0;
}
because a)
when you define an integer array is by default filled with the value zero 0
and b)
You are always going to copy to a larger matrix so you only have to go through the original matrix (matrix m
) and pass it to the destination matrix (matrix redef_colum
).
for (int f = 0; f < m.length; f++) {
for (int c = 0; c < m[f].length; c++) {
redef_colum[f][c] = m[f][c];
}
}
Now, this is valid if and only if, the origin matrix has data, because if it is empty (the whole matrix is zero) it is not required to do this copying.
3 - You must make the same changes in the public static int[][] redef_fila(int m[][])
method
4 - To traverse the matrix and store its values in a single String variable, it is preferable that you use a StringBuilder
.
StringBuilder result = new StringBuilder();
for (int x = 0; x < mat.length; x++) {
for (int y = 0; y < mat[x].length; y++) {
result.append(mat[x][y]);
result.append(" ");
}
result.append("\n") ;
}
JOptionPane.showMessageDialog(null, result.toString());