[英]Java: find sum of 2d array of numbers
Ok, so this sounds simple. 好的,这听起来很简单。 but its been playing with my head for a little while now. 但是它已经在我头上玩了一段时间了。
I need to create a method which finds the sum of a 2d array of integers. 我需要创建一个方法来查找2d整数数组的总和。
I need to create the method: 我需要创建方法:
public static int sum(int[][] array)
this is what i done so far: 这是我到目前为止所做的:
public static int sum(int[][] array){
int sum1 = 0;
for (int i : array)
sum1 += i;
return sum1;
}
But I'm getting an error 'incompatible types required int[] found int.'
但是我收到一个错误消息'incompatible types required int[] found int.'
. 。
Anyone that can help me complete this challenge? 有谁可以帮助我完成这项挑战?
Thanks. 谢谢。 Edit: an example array would be: 编辑:一个示例数组将是:
3 -1 4 0
5 9 -2 6
5 3 7 -8
for now they will always be of this format (4x3). 目前,它们将始终采用这种格式(4x3)。
Since array
is 2 dimensional, you cannot specify int i: array
in the for
loop. 由于array
是二维的,因此无法在for
循环中指定int i: array
。 Modify your code like this: 像这样修改您的代码:
public static int sum(int[][]array){
int sum1 = 0;
for (int[] arr : array)
for(int i: arr)
sum1+=i;
return sum1;
}
EDIT : 编辑 :
To store sum of each row, make use of an integer array. 要存储每一行的总和,请使用整数数组。
public static int[] sum(int[][]array){
int sum = 0;
int sumOfRow[] = new int[array.length];
for(int i=0;i<array.length;i++){
sum=0;
for(int num: array[i]){
sum1+=num;
}
sumOfRow[i] = sum;
}
return sumOfRow;
}
Your array
declared as int[][]
, which really means an array of int[]
. 您的array
声明为int[][]
,这实际上意味着一个int[]
数组。 That is, elements of array
have type int[]
, which are themselves arrays (this is why array
is a "2D" array). 也就是说, array
元素具有int[]
类型,它们本身就是数组(这就是为什么array
是“ 2D”数组)的原因。 When you write a for-each loop, make sure that your types match up: 在编写for-each循环时,请确保您的类型匹配:
for (int[] innerArray : array) {
// do things with innerArray, which is a 1D int[]
}
The full solution looks like this: 完整的解决方案如下所示:
public static int sum(int[][] array) {
int sum = 0;
for (int[] innerArray : array)
for (int i : innerArray)
sum += i;
return sum;
}
The logic inside the method should be: 方法内部的逻辑应为:
for (int[] i : array)
for(int num : i)
sum1+=num;
i
is a type int[]
not an int
. i
是int[]
类型,而不是int
。 The second loop is to enumerate over the array i
enabling us to access the numbers in there and add it to the sum1
variable. 第二个循环是枚举数组i
使我们能够访问其中的数字并将其添加到sum1
变量中。
Further, as of Java-8, you can accomplish the task at hand as follows: 此外,从Java-8开始,您可以完成以下任务:
public static int sum(int[][]array){
return Arrays.stream(array)
.flatMapToInt(Arrays::stream)
.sum();
}
You can not +
an array directly, see jls : 您不能直接+
数组,请参见jls :
If the type of either operand of a + operator is String , then the operation is string concatenation. 如果+运算符的任何一个操作数的类型为String ,则该操作为字符串连接。
Otherwise, the type of each of the operands of the + operator must be a type that is convertible (§5.1.8) to a primitive numeric type , or a compile-time error occurs. 否则,+运算符的每个操作数的类型必须是可转换(第5.1.8节)为原始数字类型的类型 ,否则会发生编译时错误。
So, if none of the operands is String
, the additive operator is only appliable for: 因此,如果所有操作数都不是String
,则加法运算符仅适用于:
You can achieve this by nested loops: 您可以通过嵌套循环来实现:
public static int sum2D(int[][] matrix){
int sum = 0;
for (int[] array : matrix) {
for (int element : array) {
sum += element;
}
}
return sum;
}
or create another method to calculate the sum of 1D array: 或创建另一种方法来计算一维数组的总和:
public static int sum2D(int[][] matrix){
int sum = 0;
for (int[] array : matrix)
sum += sum1D(array);
return sum;
}
public static int sum1D(int[] array){
int sum = 0;
for (int number : array)
sum += number;
return sum;
}
public static int sum(int[][] array)
{
int sum1 = 0;
for (int row=0; row < array.length; ++row)
{
for(int col=0; col<array[row].length; ++col)
{
sum1 = sum1 + array[row][col];
}
} return sum1;
}
more information can be found here: Finding the sum of the values in a 2D Array in C# 在这里可以找到更多信息: 在C#中查找2D数组中值的总和
logic is still sound 逻辑仍然合理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.