Java 如何在同时具有正值和负值的数组中找到最大负值?

Java 如何在同时具有正值和负值的数组中找到最大负值?,java,arrays,negative-number,Java,Arrays,Negative Number,我需要返回最大的负值,如果没有负值,我需要返回零。 以下是我所拥有的: public int greatestNegative(int[] list) { for (int i = 0; i < list.length; i++) { if (list[i] < 0) negativeNumbers ++; } int j = list.length - 1; while (j >= 0) {

我需要返回最大的负值,如果没有负值,我需要返回零。 以下是我所拥有的:

public int greatestNegative(int[] list) {


    for (int i = 0; i < list.length; i++) {


       if (list[i] < 0)
           negativeNumbers ++;
    }

    int j = list.length - 1;

    while (j >= 0) {
       if (list[j - negativeNumbers] < 0) {
        list[j] = 0;
        list[j - 1] = list[j - negativeNumbers];
        negativeNumbers--;
        j--;
       }
       else{
        list[j] = list[j - negativeNumbers];
        j--;
     }
  }

}

你得试试这个

public int greatestNegative(int[] list) {
    int negNum = 0;
    for(int i=0; i<list.length; i++) {
        if(list[i] < negNum){
            negNum = list[i];
        }
    }
    return negNum;
}


public int largNegative(int[] list) {
    int negNum = 0;
    boolean foundNeg = false;
    for(int i=0; i<list.length; i++) {
        if(list[i] < negNum && !foundNeg){
            foundNeg = true;
            negNum = list[i];
        } else if(foundNeg && list[i] < 0 && negNum < list[i]) {
            negNum = list[i];
        }
    }
    return negNum;
}

下面是返回最小负数的代码

public static int greatestNegative(int[] list) {
        int negativeNumbers = 0;
        for (int i = 0; i < list.length; i++) {
           if (list[i] < 0 && list[i] < negativeNumbers)
               negativeNumbers  = list[i];
        }

        return negativeNumbers;
    }
输入:1,2,-3,5,0,-6

输出:-6

输入:1,2,3,5,0,6

输出:0


只需在附加条件下查找最大值

public static int greatestNegative(int[] list) {
    int max = Integer.MIN;
    boolean set = false;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < 0 && list[i] > max) {
             max = arr[i];
             set = true;
        }
    }
    if (!set)
        max = 0;
    return max;
}

如果需要最大负数,则对数组进行排序,并搜索第一个负数

import java.util.Arrays;

class Main {

    public static void main(String[] args) {
        int arr[] = { 2, 4, 1, 7,2,-3,5,-20,-4,5,-9};
        System.out.println(greatestNegative(arr));
    }

    private static int greatestNegative(int[] arr) {
        Arrays.sort(arr);
        for (int i = arr.length - 1; i >= 0; i--) {
            if (isNegative (arr[i])) {
                return arr[i];
            }
        }
        return 0;
    }

    private static boolean isNegative (int i) {
        return i < 0;
    }
}
输出:-3


请检查以下代码,它将 首先从数组中计算小数值, 那么检查一下它是阳性的吗?如果是,则返回0,否则返回负数

public static int greatestNegative(int[] list) 
{
    int negativeNumbers = Integer.MAX_VALUE;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < negativeNumbers)
                negativeNumbers  = list[i];
    }

    if(negativeNumbers  >=0)
         return 0;
    else
         return negativeNumbers;

}

首先将maxNegative值设置为0。然后给你遇到的第一个负数赋值。之后,只分配较高的负数。如果没有负数,那么maxNegative仍然是零

public static void main(String[] args) {
    int arr[] = {2, -1, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
    int maxNegative = 0;
    for (int i = 0; i < arr.length; i++) {
        if (maxNegative == 0 && arr[i] < maxNegative) {
            // Set the first negative number you come across
            maxNegative = arr[i];
        } else if (maxNegative < arr[i] && arr[i] < 0) {
            // Set greater negative numbers
            maxNegative = arr[i];
        }
    }
    System.out.println(maxNegative);
}
爪哇8 然后是流,它允许您使用一行代码来实现这一点

public static void main(String[] args) {
    int arr[] = {2, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
    int maxNegative = Arrays.stream(arr).filter(a -> a < 0).max().orElse(0);
    System.out.println(maxNegative);
}

您只需将此问题视为两个步骤:

只考虑列表[0]中的负值。 在负值内的循环中,如果结果==0或值>结果,则更新当前结果。 代码:


没有返回类型,这段方法将不会编译。@UnknownOctopus但是我们在方法实现中返回的位置?这是编译吗?你什么都不退,好的。对于你们发布的所有选项,我很感激,但这就是我所说的验证代码的案例。这是仅有的两个效果良好的案例:最大负{1,21,23,-17,7,-17,14,4,5}→ -17最大负{9,2,3,4,7}→ 这里有一些不起作用的:最大负{0,-9,-12,6,10,-15,7}→ -这个答案是-15,应该是-9谢谢!这种方法非常有效。这是一个很好的解释!
public static void main(String[] args) {
    int arr[] = {2, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
    int maxNegative = Arrays.stream(arr).filter(a -> a < 0).max().orElse(0);
    System.out.println(maxNegative);
}
-3
public int greatestNegative(int[] list) {
    int result = 0;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < 0) {
            if (result == 0 || list[i] > result) {
                result = list[i];
            }
        }
    }
    return result;
}