java 如何求和矩阵中的一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41926972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sum a row in a matrix
提问by Bry
Write the method:
写方法:
public int sumRow(int[][] matrix, int row)
that sums row row
in the 2D array called matrix.
对row
称为矩阵的二维数组中的行求和。
Given:
鉴于:
public void run()
{
System.out.println(sumRow(new int[][]{{70,93,68,78,83},{68,89,91,93,72},{98,68,69,79,88}}, 2));
System.out.println(sumRow(new int[][]{{1,1,1}, {2,2,2}, {3,3,3}}, 0));
System.out.println(sumRow(new int[][]{{2,4,6,8,10}, {1,2,3,4,5}, {10,20,30,40,50}}, 2));
}
So far I have:
到目前为止,我有:
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix.length; j++)
{
sum = sum + matrix[j][i];
}
}
return sum;
}
The outputs I get are 714, 18, and 78 when they should be 402, 3, and 150. What am I doing wrong?
我得到的输出是 714、18 和 78,而它们应该是 402、3 和 150。我做错了什么?
回答by Chris Gong
You're currently trying to sum all of the elements in the 2D array when you were asked to sum a specific row within the 2D array. In this case, you only need one for loop to traverse a single row like you would traverse a single array. The loop would start at the first element, matrix[row][0]
and run until the last element, matrix[row][matrix[row].length - 1]
since matrix[row].length
is the number of columns/elements in that specific row of the matrix. Therefore, matrix[row].length - 1
would be the index of the last element in matrix[row]
. Here's what it should look like,
当您被要求对二维数组中的特定行求和时,您当前正在尝试对二维数组中的所有元素求和。在这种情况下,您只需要一个 for 循环来遍历单行,就像遍历单个数组一样。循环将从第一个元素开始,matrix[row][0]
一直运行到最后一个元素,matrix[row][matrix[row].length - 1]
因为matrix[row].length
是矩阵特定行中的列/元素数。因此,matrix[row].length - 1
将是 中最后一个元素的索引matrix[row]
。它应该是这样的
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix[row].length; i++)
{
sum += matrix[row][i];
}
return sum;
}
回答by Selim Ajimi
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
int colSize = matrix[row].length;
for(int j = 0; j < colSize; j++){
sum += matrix[row][j];
}
return sum;
}
HINT
暗示
Length of row:
长排:
int row = matrix.length;
Length of column:
柱长:
int col = matrix[0].length;
回答by Ollaw
In the internal loop, modify the condition to:
在内部循环中,修改条件为:
for(int j = 0; j < matrix[i].length; j++)
and then switch i
and j
in the sum
然后在总和中切换i
和j
回答by Mohsen_Fatemi
there is a problem with your second for
loop's condition , matrix.length
is the length of first dimension , for the second dimension it looks like matrix[i].length
你的第二个for
循环的条件有问题,matrix.length
是第一个维度的长度,对于第二个维度,它看起来像matrix[i].length
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[i].length; j++){
sum = sum + matrix[i][j];
}
}
i prefer to use sum+=matrix[i][j]
instead of sum = sum + matrix[i][j]
我更喜欢使用sum+=matrix[i][j]
而不是sum = sum + matrix[i][j]
calculating for onerow :
计算一个行:
for(int j = 0; j < matrix[row].length; j++){
sum = sum + matrix[row][j];
}
just notethat row's range is from 0
to matrix.length-1
只是注意到该行的范围是从0
到matrix.length-1
回答by Locke
You need to reference the second dimension of the array for j.
您需要为 j 引用数组的第二个维度。
//ex: first dimension is matrix.length
//second dimension is matrix[any index in the first dimension].length
//and this cycle would continue with more and more [num] on the end
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix**[0]**.length; j++)
{
sum = sum + matrix[j][i];
}
}
return sum;
}