简体   繁体   English

java我怎么读一个负int作为积极的

[英]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();

Change all numbers to positive numbers. 将所有数字更改为正数。 This can be done using Math.abs(int) .Check the link . 这可以使用Math.abs(int) 。检查链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中快速计算2的大正或负幂? - How do I quickly calculate a large positive, or negative, power of 2 in Java? 如何在数字序列中从正整数和负整数交替 - how do i alternate from positive int and negative int in a number sequence 如何检查零是正数还是负数? - How do I check if a zero is positive or negative? Java 数学 function 将正整数转换为负数,将负数转换为正数? - Java math function to convert positive int to negative and negative to positive? 如何在 java 中将短路转换为整数而不将其转换为负数 - How do I convert a short to an int without turning it into a negative in java 我如何使这些值既正面又负面? - How do I make these values both positive and negative? 如何在JSTL中舍入正负十进制? - How do I round a positive and negative decimal in JSTL? 如果我不知道变量是正/负,如何在不调用函数或不使用if的情况下使其变为正数? - If I don't know if a variable is positive/negative, how do I make it positive without calling a function or using if? 将两条短裤打包成一个整数,处理负面和正面 - Packing two shorts into one int, dealing with negative and positive 如何让我的正整数变为负数并保持负数直到我的行动结束? - How do I make my positive integer become negative and stay negative until the end of my action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM