Java 查找二维数组的行之和并在数组中返回它
我正在尝试完成一个AP CS FRQ问题。我写的代码,但它不工作。我哪里搞砸了 编写一个静态方法rowSums,该方法计算给定二维空间中每一行的总和 数组,并以一维数组返回这些和。该方法只有一个参数,即二维参数 int值的数组arr2D。数组按行主顺序排列:arr2D[r][c]是条目 在r行和c列。该方法返回一个一维数组,每个数组行有一个条目 arr2D,使得每个条目都是arr2D中对应行的总和。作为提醒,每行 二维数组是一维数组Java 查找二维数组的行之和并在数组中返回它,java,arrays,Java,Arrays,我正在尝试完成一个AP CS FRQ问题。我写的代码,但它不工作。我哪里搞砸了 编写一个静态方法rowSums,该方法计算给定二维空间中每一行的总和 数组,并以一维数组返回这些和。该方法只有一个参数,即二维参数 int值的数组arr2D。数组按行主顺序排列:arr2D[r][c]是条目 在r行和c列。该方法返回一个一维数组,每个数组行有一个条目 arr2D,使得每个条目都是arr2D中对应行的总和。作为提醒,每行 二维数组是一维数组 `public static int[]行和(int[][]a
`public static int[]行和(int[][]arr2D){
整数2=0;
int a[]=新int[arr2D.length];
对于(int x=0;x您的赋值是向后的,您应该使用以下方法存储2D数组的每个元素:
total2 = arr2D[x][n];
不是这个:
arr2D[x][n] = total2;
完整代码:
for (int x=0; x < arr2D.length; x++) {
for (int n=0; n < arr2D[x].length; n++) {
total2 = arr2D[x][n];
a[x] = a[x] + total2;
}
}
for(int x=0;x
您需要在外循环内重置total2
,并在内循环结束后设置值
int a[] = new int[arr2D.length];
for(int x=0; x<arr2D.length; x++){
int total2 = 0;
for(int n=0; n<arr2D[x].length;n++){
total2 += arr2D [x][n];
}
a[x] = total2;
}
编写好的代码包括好的注释和好的变量名选择。让我们首先逐行注释代码,以便更好地了解发生了什么:
public static int[] rowSums(int[][] arr2D){
// A variable which is always 0
int total2 = 0;
// The actual output:
int a[] = new int[arr2D.length];
// For each row..
for(int x=0; x<arr2D.length; x++){
// For each column..
for(int n=0; n<arr2D[x].length;n++){
// Put 0 into the 2D array (this line is backwards):
arr2D[x][n] = total2;
// Add the 'total' (always 0) into the current output
a[x] = a[x] + total2;
}
}
// Return the output
return a;
}
公共静态int[]行和(int[]arr2D){
//始终为0的变量
整数2=0;
//实际产出:
int a[]=新int[arr2D.length];
//每行。。
对于(int x=0;xarr2D[x][n]=total2;//您正在将0分配给arr2D[x][n]
将其更改为total2=arr2D[x][n]
而且它会起作用!!有些人会认为好的代码甚至不需要注释:-)(也许Javadoc还可以)。@TimBiegeleisen我完全同意-对于刚开始的人来说,它们让所有东西都更容易阅读(并且也有助于命名选择).嗯,有点太啰嗦了me@ScaryWombat由于OP存在初学者范围界定问题以及行数倒转问题,我认为他们需要尽可能清楚地了解他们的代码在做什么。@LukeBriggs和一只橡皮鸭@Luke不,你不知道bcz每次都会给total2分配新值。请尝试!!
for (int x=0; x < arr2D.length; x++) {
for (int n=0; n<arr2D[x].length; n++) {
a[x] = a[x] + arr2D[x][n];
}
}
public static int[] rowSums(int[][] arr2D){
// A variable which is always 0
int total2 = 0;
// The actual output:
int a[] = new int[arr2D.length];
// For each row..
for(int x=0; x<arr2D.length; x++){
// For each column..
for(int n=0; n<arr2D[x].length;n++){
// Put 0 into the 2D array (this line is backwards):
arr2D[x][n] = total2;
// Add the 'total' (always 0) into the current output
a[x] = a[x] + total2;
}
}
// Return the output
return a;
}
public static int[] rowSums(int[][] arr2D){
// The actual output:
int totals[] = new int[arr2D.length];
// For each row..
for(int row=0; row<arr2D.length; row++){
// For each column..
for(int col=0; col<arr2D[row].length;col++){
// Get the column value:
int columnValue = arr2D[row][col];
// Add the column amount into the total:
totals[row] = totals[row] + columnValue;
}
}
// Return the output
return totals;
}
public static int[] rowSums(int[][] arr2D){
int totals[] = new int[arr2D.length];
for(int row=0; row<arr2D.length; row++){
for(int col=0; col<arr2D[row].length;col++){
int columnValue = arr2D[row][col];
totals[row] = totals[row] + columnValue;
}
}
return totals;
}