简体   繁体   中英

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.

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:

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:

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):

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. You need it on this line:

int sum = IntStream.of(array[i]).sum();

to take array[i] and make it absolute. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM