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

时间:2018-10-25 18:12:54

标签: 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", "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);
    }
}

6 个答案:

答案 0 :(得分:2)

实际上,您不需要每次迭代都跟踪选定的月份。假设月份与索引中的数组元素相关,那么您所需要做的就是找出最大元素的索引-您甚至不需要跟踪最大值:

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]);
    }
}

答案 1 :(得分:1)

使用流:

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]);
}

答案 2 :(得分:0)

如果可以使用Map,则它非常简单。创建一个HashMap<String, Integer>,其中key是月份,value是计数。

然后您可以遍历此地图并找到其值最大的键,值对。

解决方案:

    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();

答案 3 :(得分:0)

仅使用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]);

答案 4 :(得分:0)

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

        Integer[] copyArr = array.clone();
        Arrays.sort(copyArr);
        int largestNum = copyArr[copyArr.length-1];
        int index = Arrays.binarySearch(array, largestNum);
        System.out.println(month[index]);
     }

输出-> 6月

答案 5 :(得分:-1)

在Java中,我们有一个枚举,您可以在其中使用常量和与其关联的值。 请参阅https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

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

现在,在任何其他方法中,您都可以遍历枚举值,然后找到最大值。 因此,假设Months是枚举的名称,那么您可以为此使用简单的for-each循环:

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

注意:ENUMS通常用于常量。因此,不确定所描述的问题是否总是每个月都有常量值,或者可能会有所不同。如果有所不同,则可以使用{ {1}}或streams,如已经给出的答案所述。