【发布时间】:2012-01-06 01:46:28
【问题描述】:
在使用 hashmap 查找最常见的值时,如果输入的数据集包含重复值,则代码运行良好,另一方面,如果数据集没有重复值,则在这种情况下它也返回模式值:(
我想返回没有可用的模式。 请帮忙
public void onMode(View Button){
EditText inp = (EditText) findViewById(R.id.EditText01);
float[] input = new float[uno];
float answer = 0;
input = points;
answer = getMode(input);
Float floatInput2 = new Float (answer);
String newinput2 = floatInput2.toString();
inp.setText("Your required Mode is "+newinput2);
}
public static float getMode(float[] values) {
HashMap<Float,Float> freqs = new HashMap<Float,Float>();
for (float val : values) {
Float freq = freqs.get(val);
freqs.put(val, (freq == null ? 1 : freq+1));
}
float mode = 0;
float maxFreq = 0;
for (Map.Entry<Float,Float> entry : freqs.entrySet()) {
float freq = entry.getValue();
if (freq > maxFreq) {
maxFreq = freq;
mode = entry.getKey();
}
}
return mode;
}
我想在数据集中找到重复次数最多的值,或者如果数据集中不包含任何重复值,那么它将返回“不存在模式”
【问题讨论】: