Например, рассмотрим следующий код.
Код: Выделить всё
// Array of doubles to "hold" the array.
private double matrix[][];
public int getCols() {
// Compute the number of columns in a matrix.
}
public int getRows() {
// Compute the number of rows in a matrix.
}
// Compute the sum of all elements in the matrix.
public double sum() {
double result = 0;
for (int r = 0; r < getRows(); r++) {
for (int c = 0; c < getCols(); c++) {
result += this.matrix[r][c];
}
}
return result;
}
Код: Выделить всё
public double sum() {
double result = 0;
int numRows = getRows();
int numCols = getCols();
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
result += this.matrix[r][c];
}
}
return result;
}
Подробнее здесь: https://stackoverflow.com/questions/679 ... structures
Мобильная версия