You can use nested loops to assign values:
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
matrix[row][col] = row + col;
}
}
//matrix[0].length gives the number of columns in 2D arrays.
//matrix.length gives the number of rows in 2D arrays.
No comments:
Post a Comment