简体   繁体   English

一次显示字符串和Int数组

[英]Display String and Int Array at once

I am not sure how to display a string and int array at the same time so that it will display the maximum value of the array with the specific month 我不知道如何同时显示字符串和int数组,以便它显示具有特定月份的数组的最大值

Global:
static double[] set2014 = new double[6];
static String[] months = new String[6];

Here is the method for calculating the maximum value: 以下是计算最大值的方法:

public static void max(){
    initialise();
    double max = set2014[0];

    for(int i = 1; i < set2014.length; i++){
        if(set2014[i] > max){
            max = set2014[i];
        }
    }
    System.out.println("------------------");
    System.out.println("Largest figure is " + max);
}

For example, the output will be: Largest figure is: March 204566 例如,输出将是:最大的数字是:204566年3月

Maintaining two arrays that are linked by index is what I call Object Denial. 维护两个通过索引链接的数组就是我所说的Object Denial。 Consider creating a class that contains both month and value. 考虑创建一个包含月份和值的类。

public interface MonthValue { //class or interface, I just didn't want to type out the simple implementation
   String getMonth();
   double getDouble();
}

//set2014 now needs to contain MonthValues
MonthValue max = set2014[0];

for(int i = 1; i < set2014.length; i++){
    MonthValue current = set2014[i];
    if(current.getValue() > max.getValue()){
        max = current;
    }
}

System.out.println("Largest figure is " + max.getValue());
System.out.println("In month " + max.getMonth());

But to answer your question as is: 但要回答你的问题:

You can keep track of the index instead: 您可以改为跟踪索引:

int maxMonthIndex = 0;

for(int i = 1; i < set2014.length; i++){
    if(set2014[i] > set2014[maxMonthIndex]){
        maxMonthIndex = i;
    }
}

System.out.println("Largest figure is " + set2014[maxMonthIndex]);
System.out.println("In month " + months[maxMonthIndex]);

By the way, set is a bad name for an array. 顺便说一句, set是一个错误的数组名称。 Sets are unordered collections, arrays have an order. 集合是无序集合,数组有顺序。

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

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