将子脚本从一个数组匹配到另一个数组。

时间:2022-09-25 08:06:15

I'm having this issue with arrays. Maybe I'm approaching this in a weird way, you guys let me know. I have two arrays:

我用数组来解决这个问题。也许我用一种奇怪的方式接近这个,你们让我知道。我有两个数组:

String[] months = {"January","February","March","April","May",
                   "June","July","August","September",
                   "October","November","December"};
double[] rainfall ={4.22, 3.18, 3.03, 3.52, 4.54, 5.55, 
                    4.71, 4.35, 5.26, 5.46, 4.78, 4.09};

These numbers represent rainfall. I run the algorithm to find the Most and the Least from double[] rainfall, and it gives me the number 3.03, which is march. My question is, How can I correlate that subscript to the one from the array String[] months so that it shows me the name of the month as well? Should I make a stacked array instead?

这些数字代表降雨。我运行算法,找到了最多和最小的双[]降水,它给了我3。03,也就是3月。我的问题是,如何将子脚本与数组字符串中的一个关联起来[]月,这样它就可以显示月份的名称了?我应该做一个堆叠数组吗?

3 个解决方案

#1


-1  

Pretty simple solution is to search for the index of your smallVal and bigVal.

非常简单的解决方法是搜索您的smallVal和bigVal的索引。

for(int i = 0; i<rainfall.size(); i++){
   if(maxRain == rainFall[i])
         maxIndex = i;
   if(minRain == rainFall[i])
         minIndex = i;

}

smallMonth = months[minIndex];
bigMonth = months[maxIndex];

#2


1  

I suggest to change your data structure to map:

我建议更改您的数据结构,以映射:

Map<String, Double> rainfallMap = new TreeMap<>();
rainfallMap.put("January", 4.22);
rainfallMap.put("February", 3.18);
rainfallMap.put("March", 3.03);
rainfallMap.put("April", 3.52);
rainfallMap.put("May", 4.54);
rainfallMap.put("June", 5.55);
rainfallMap.put("July", 4.71);
rainfallMap.put("August", 4.35);
rainfallMap.put("September", 5.26);
rainfallMap.put("October", 5.46);
rainfallMap.put("November", 4.78);
rainfallMap.put("December", 4.09);

Now you can do this (Java 8):

现在您可以这样做(Java 8):

Entry<String, Double> minEntry = rainfallMap.entrySet().stream()
    .min((e1, e2) -> Double.compare(e1.getValue(), e2.getValue()))
    .get();

System.out.println(String.format("Minimal rainfall was %.2f in %s",
    minEntry.getValue(), minEntry.getKey()));

EDIT:

编辑:

@Tagir Valeev suggested better retrieval of minimal entry:

@Tagir Valeev建议更好地检索最小条目:

Entry<String, Double> minEntry = 
    Collections.min(rainfallMap.entrySet(), Map.Entry.comparingByValue());

I compared both approached with JMH and @Tagir Valeev's suggestion is definitely better:

我比较了与JMH和@Tagir Valeev的建议,肯定更好:

 Benchmark                          Mode  Cnt    Score    Error  Units
 Rainfall.getMinRaifallCollections  avgt    5   73.654 ±  3.768  ns/op
 Rainfall.getMinRainfallStreams     avgt    5  126.808 ± 10.559  ns/op

#3


0  

Well, you'll need a linear search to get the lowest value in the second array. When you find the lowest value, save its index. You'll use the index to find the corresponding month:

你需要一个线性搜索来得到第二个数组中的最小值。当你找到最小值时,保存它的索引。您将使用该索引查找对应的月份:

int minIndex = 0;

for(int i = 1; i < rainfall.length; i++)
{
    if(rainfall[i] < rainfall[minIndex])
    {
        minIndex = i;
    }
}

string month = months[minIndex];

#1


-1  

Pretty simple solution is to search for the index of your smallVal and bigVal.

非常简单的解决方法是搜索您的smallVal和bigVal的索引。

for(int i = 0; i<rainfall.size(); i++){
   if(maxRain == rainFall[i])
         maxIndex = i;
   if(minRain == rainFall[i])
         minIndex = i;

}

smallMonth = months[minIndex];
bigMonth = months[maxIndex];

#2


1  

I suggest to change your data structure to map:

我建议更改您的数据结构,以映射:

Map<String, Double> rainfallMap = new TreeMap<>();
rainfallMap.put("January", 4.22);
rainfallMap.put("February", 3.18);
rainfallMap.put("March", 3.03);
rainfallMap.put("April", 3.52);
rainfallMap.put("May", 4.54);
rainfallMap.put("June", 5.55);
rainfallMap.put("July", 4.71);
rainfallMap.put("August", 4.35);
rainfallMap.put("September", 5.26);
rainfallMap.put("October", 5.46);
rainfallMap.put("November", 4.78);
rainfallMap.put("December", 4.09);

Now you can do this (Java 8):

现在您可以这样做(Java 8):

Entry<String, Double> minEntry = rainfallMap.entrySet().stream()
    .min((e1, e2) -> Double.compare(e1.getValue(), e2.getValue()))
    .get();

System.out.println(String.format("Minimal rainfall was %.2f in %s",
    minEntry.getValue(), minEntry.getKey()));

EDIT:

编辑:

@Tagir Valeev suggested better retrieval of minimal entry:

@Tagir Valeev建议更好地检索最小条目:

Entry<String, Double> minEntry = 
    Collections.min(rainfallMap.entrySet(), Map.Entry.comparingByValue());

I compared both approached with JMH and @Tagir Valeev's suggestion is definitely better:

我比较了与JMH和@Tagir Valeev的建议,肯定更好:

 Benchmark                          Mode  Cnt    Score    Error  Units
 Rainfall.getMinRaifallCollections  avgt    5   73.654 ±  3.768  ns/op
 Rainfall.getMinRainfallStreams     avgt    5  126.808 ± 10.559  ns/op

#3


0  

Well, you'll need a linear search to get the lowest value in the second array. When you find the lowest value, save its index. You'll use the index to find the corresponding month:

你需要一个线性搜索来得到第二个数组中的最小值。当你找到最小值时,保存它的索引。您将使用该索引查找对应的月份:

int minIndex = 0;

for(int i = 1; i < rainfall.length; i++)
{
    if(rainfall[i] < rainfall[minIndex])
    {
        minIndex = i;
    }
}

string month = months[minIndex];