I'm trying to prepare data to be used inside of a histogram. I want 2 arrays, one is all the temperatures from my highest and lowest collected temperature. The second array will contain the frequency of each temperature.
int difference
is the difference from the lowest and highest temp
Array temp
contains the temperatures collected
HashMap map
contains the frequencies collected of each temp
Array tempGaps
contains the temperatures + every other temp that was not collected
Array finalTemps
contains the frequency of each temp.
The goal I hope to achieve is two arrays, one with all the temperatures, one with the frequencies of each and their index value is what corresponds them to eachother.
public void fillGaps() {
int j = 0;
tempGaps = new int[difference];
finalTemps = new int[difference];
for (int i = 0; i < difference; i++) {
tempGaps[i] = temp[0] + i;
if (tempGaps[i] == temp[j]) {
finalTemps[i] = map.get(new Integer(tempGaps[i]));
j++;
} else {
finalTemps[i] = 0;
}
}
}output: https://pastebin.com/nFCZXFyp
Output:
7 ->1 9 ->1 10 ->1 12 ->1 14 ->2 15 ->1 16 ->1 18 ->2 19 ->1 21 ->1
TEMP GAPS
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
FINAL TEMPS
1 0 1 1 0 1 0 2 0 0 0 0 0 0 0
My finalTemp output stops after 14 degrees - occurs 2 times. It does this after any data set I put in which has a frequency of more than 1. Please help! Thanks!!
I don't believe you need the j
variable. It seems to make things confusing, especially because the for loop is only a single pass-through. Instead, fetch the value from the map. If null, assign 0, otherwise, assign the value.
public void fillGaps() {
tempGaps = new int[difference];
finalTemps = new int[difference];
for (int i = 0; i < difference; i++) { //navigate through every temperature
tempGaps[i] = temp[0] + i; //assign the current temperature
Integer frequency = map.get(new Integer(tempGaps[i])); //fetch the frequency
finalTemps[i] = frequency == null ? 0 : frequency; //assign 0 if null, otherwise frequency if not null
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.