如何遍历数组并找到最高值:Java

如何遍历数组并找到最高值:Java,java,arrays,Java,Arrays,在下面的代码中,我应该找到与最大数字相关的月份 贝娄是我试过的。然而,我相信有一种更为贴切的方式来写这篇文章。我怎样才能做到这一点 public class HelloWorld{ static int[] array = {3,6,7,3,2,30,9,13,12,1,2,1}; static String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "Septemb

在下面的代码中,我应该找到与最大数字相关的月份

贝娄是我试过的。然而,我相信有一种更为贴切的方式来写这篇文章。我怎样才能做到这一点

public class HelloWorld{

static int[] array = {3,6,7,3,2,30,9,13,12,1,2,1};
static String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

static int greatestVal = 0;
static int greatestVal2 = 0;
static String monthChosen = "";

int integer = 0;
static int num = 0;

     public static void main(String []args){
            int number = -1;
            for (int i = 0; i<=11; i++) {
                int currentValue = array[i];
                number += 1;
                //System.out.println(currentValue);
                for (int index = 0; index <=11; index++) {
                    if (currentValue > array[index]) {
                        greatestVal = currentValue;
                        // System.out.println(currentValue +">"+ array[index]);
                        if (greatestVal > greatestVal2) {
                            greatestVal2 = greatestVal;
                            monthChosen = month[number];
                        }
                    } else {
                        // System.out.print("Hgfhdssdgfadkhfdshkjhads");
                    }
                }
        }
            System.out.println(greatestVal2 + " greatest month is: " + monthChosen);
    }
}
公共类HelloWorld{
静态int[]数组={3,6,7,3,2,30,9,13,12,1,2,1};
静态字符串[]月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
静态int greatestVal=0;
静态int greatestVal2=0;
静态字符串monthChosen=“”;
整数=0;
静态int num=0;
公共静态void main(字符串[]args){
整数=-1;
for(int i=0;i最大值2){
greatestVal2=greatestVal;
monthChosen=月[数];
}
}否则{
//系统输出打印(“HGFHDSDGDGFADKHFDSHKJHADS”);
}
}
}
System.out.println(greatestVal2+“最大月份为:”+monthChosen);
}
}

如果你能使用
地图
,那就非常简单了。创建一个
HashMap
,其中
键是月份,
值是计数

然后,您可以迭代此映射并找到值最大的键、值对

解决方案:

    Map<String, Integer> monthMap = new HashMap<>();
    // initializing the map
    for (int i = 0; i < 12; i++)
        monthMap.put(month[i], array[i]);

    // finding <k, v> pair with highest entry
    Entry<String, Integer> entry = monthMap.entrySet().stream()
             .max(Map.Entry.comparingByValue(Comparator.comparingInt(i -> i.intValue())))
             .get();
Map monthMap=newhashmap();
//初始化地图
对于(int i=0;i<12;i++)
monthMap.put(月[i],数组[i]);
//查找具有最高入口的配对
Entry=monthMap.entrySet().stream()
.max(Map.Entry.comparingByValue(Comparator.comparingit(i->i.intValue()))
.get();

仅使用2个阵列即可简化解决方案

    int max = Arrays.stream(array).max().getAsInt();
    int index = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == max) {
            index = i;
        }
    }
    System.out.println(array[index] + " greatest month is: " + month[index]);
int max=Arrays.stream(array.max().getAsInt();
int指数=0;
for(int i=0;i
使用流:

static int[] array = {3,6,7,3,2,30,9,13,12,1,2,1};
static String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public static void main(String[] args) {
    int index = IntStream.range(0, array.length)
            .reduce(0, (a, b) -> array[a] >= array[b] ? a : b);
    System.out.println(array[index] + " greatest month is: " + month[index]);
}

output->June

您实际上不需要跟踪每次迭代所选的月份。假设月份通过索引与数组元素相关,则只需找出最大元素的索引,甚至不需要跟踪最大值:

public class HelloWorld {
    static int[] array = {3, 6, 7, 3, 2, 30, 9, 13, 12, 1, 2, 1};
    static String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    public static void main(String[] args) {
        int index = 0;
        for (int i = 1; i < array.length; i++)
            if (array[index] < array[i])
                index = i;
        System.out.println(array[index] + " greatest month is: " + month[index]);
    }
}
公共类HelloWorld{
静态int[]数组={3,6,7,3,2,30,9,13,12,1,2,1};
静态字符串[]月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
公共静态void main(字符串[]args){
int指数=0;
for(int i=1;i
在Java中,我们有枚举,您可以在其中使用常量和与之关联的值。 提及

使用这种方法,您可以定义一个枚举,其中名称为month,值可以与之关联

现在,在任何其他方法中,您都可以继续迭代枚举值,然后找到最大值。 因此,假设Months是枚举的名称,那么您可以为每个
循环使用一个简单的

for (Months months:Months.values()){
  //logic for comparison.find the maximum
}

注意:枚举通常用于常量。因此,不确定所描述的问题是否每个月都会有常量值,或者它可能会有所不同。如果它有所不同,则可以使用已给出答案中所述的
映射

这将使用数组二进制搜索方法获取索引然后在月数组中查找该索引
for (Months months:Months.values()){
  //logic for comparison.find the maximum
}