[英]java how do i read a negative int as a positive one
The question at hand is: 手头的问题是:
The method should compute for each row the sum of the absolute values of the elements of that row.
该方法应为每一行计算该行元素的绝对值之和。 The method should return the maximum of these sums.
该方法应返回这些总和的最大值。 For example, if applied to the test array, the method should return the value max (3+1+4+0,5+9+2+6, 5+3+7+8) = max (8,22,23) = 23.
例如,如果应用于测试数组,该方法应返回值max(3 + 1 + 4 + 0,5 + 9 + 2 + 6,5 + 3 + 7 + 8)= max(8,22,23) )= 23。
The test array: 测试数组:
3 -1 4 0
5 9 -2 6
5 3 7 -8
so far i have made a method that gets the value of all the rows and returns to me the sum in a list; 到目前为止,我已经创建了一个方法来获取所有行的值,并返回列表中的总和; however, i want it to turn negative integers into positive ones.
但是,我希望它将负整数转化为正整数。 here's my code so far:
到目前为止这是我的代码:
public static int[] rowSums(int[][]array) {
int[] a = new int[array.length];
for (int i = 0;i<array.length;i++){
int sum = IntStream.of(array[i]).sum();
a[i]=sum;
} return a;}
Output: 输出:
[6,18,7]
You can use Math#abs
(standing for absolute value) which converts any negative number into a positive number: 您可以使用
Math#abs
(代表绝对值)将任何负数转换为正数:
int sum = IntStream.of(array[i]).map(Math::abs).sum();
If you'd like to lose those for-loops, you can even just stick with streams: 如果你想丢失那些for循环,你甚至可以坚持使用流:
public static int[] rowSums(int[][] array) {
return Arrays.stream(array)
.mapToInt(i -> Arrays.stream(i).map(Math::abs).sum())
.toArray();
}
Here you can use the Map. 在这里你可以使用地图。 So The code will be like this.
所以代码就是这样的。
public static int[] rowSums(int[][]array) {
int[] a = new int[array.length];
for (int i = 0;i<array.length;i++){
int sum = IntStream.of(array[i]).map(n->{
if(n<0) {
return n*-1;
}
else {
return n;
}
}).sum();
a[i]=sum;
} return a;}
Here I just make all the negative Integers into Positive. 在这里,我只是将所有负整数变为正数。
Update this line 更新此行
int sum = IntStream.of(array[i]).sum();
to 至
int sum = IntStream.of(array[i]).map(Math::abs).sum();
And solution for whole matrix (this snippet will return 23): 整个矩阵的解决方案(这个片段将返回23):
Optional<Integer> maxVal = Arrays.stream(matrix)
.map(array -> Arrays.stream(array).map(Math::abs).sum())
.reduce(Integer::max);
And without Optional 并且没有可选
int maxVal = Arrays.stream(matrix)
.map(array -> Arrays.stream(array).map(Math::abs).sum())
.reduce(Integer::max).get();
Just use: 只需使用:
Math.abs(var)
This will turn var to a positive if it is negative. 如果它是负数,这将变为正数。
Therefore the full answer is as follows: 因此,完整的答案如下:
public static int[] rowSums(int[][]array) {
int[] a = new int[array.length];
for (int i = 0;i<array.length;i++){
int sum = IntStream.of(array[i]).map(Math::abs).sum();
a[i]=sum;
} return a;}
Use Math.abs()
It'll take any number and return it as a positive number. 使用
Math.abs()
它将获取任何数字并将其作为正数返回。 You need it on this line: 你在这条线上需要它:
int sum = IntStream.of(array[i]).sum();
to take array[i]
and make it absolute. 采取
array[i]
并使其绝对。 So 所以
IntStream.of(Math.abs(array[i]).sum();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.