【发布时间】:2019-12-12 19:14:32
【问题描述】:
我想从我的数据集中找到最接近的值。第一个值返回正确的数据,但第二个值返回最后一个最近的数据。例如,如果我有7, 11, 12, 15, 17, 20 并且我的参数值为 10,它应该返回第一个最接近的值 = 11 和第二个最接近的值 = 12。但是当我尝试时,第二个值不是 12 而是 20。第一个值将给出正确的返回但第二个最接近的值将始终显示最后一个最接近的值,而不是第二个最接近的值。
这是我的代码:
String closestPosition = null;
ArrayList<Router> wifis = db.getFriendlyWifis(building);
double min_distance = positionData.uDistance(positionsData.get(0), wifis);
closestPosition = positionsData.get(0).getName();
String res = "";
res += closestPosition + "\n" + min_distance;
res += "\n" + positionsData.get(0).toString();
int j=0;
for (int i = 1; i < positionsData.size(); i++) {
double distance = positionData.uDistance(positionsData.get(i), wifis);
res += "\n" + positionsData.get(i).getName() + "\n" + distance;
res += "\n" + positionsData.get(i).toString();
if (distance < min_distance) {
min_distance = distance;
closestPosition = positionsData.get(i).getName();
j=i;
}
}
if (min_distance == PositionData.MAX_DISTANCE){
closestPosition="OUT OF RANGE";
Toast.makeText(this,"You are out of range of the selected building",Toast.LENGTH_LONG).show();
}
result.setText("Nearest point : "+ closestPosition);
res += "\nCurrent:\n" + positionData.toString();
Log.v("Result",res);
//////////////////////////////////////////////////
min_distance = positionData.uDistance(positionsData.get(0), wifis);
String closestPosition2 = null;
closestPosition2 = positionsData.get(0).getName();
res = "";
res += closestPosition2 + "\n" + min_distance;
res += "\n" + positionsData.get(0).toString();
for (int i = 1; i < positionsData.size(); i=i+2) { if(j!=i){
double distance = positionData.uDistance(positionsData.get(i), wifis);
res += "\n" + positionsData.get(i).getName() + "\n" + distance;
res += "\n" + positionsData.get(i).toString();
closestPosition2 = positionsData.get(i).getName();//////////////////////////
if(closestPosition2.equals(closestPosition))
continue;
if (distance < min_distance) {
min_distance = distance;
closestPosition2 = positionsData.get(i).getName();
}
}
}
if (min_distance == PositionData.MAX_DISTANCE){
closestPosition2="OUT OF RANGE";
Toast.makeText(this,"You are out of range of the selected building",Toast.LENGTH_LONG).show();
}
result2.setText("Nearest point : "+ closestPosition2);
这是输出:
V/closest1:: f
V/closest2:: e
这是日志:
d
36.0
d(4.0,4.0)
UGM-Secure : -63.0
eduroam : -63.0
UGM-Hotspot : -42.0
a
64.0
a(1.0,1.0)
UGM-Hotspot : -40.0
f
17.0
f(6.0,6.0)
UGM-Secure : -65.0
eduroam : -66.0
UGM-Hotspot : -46.0
b
25.0
b(2.0,2.0)
UGM-Secure : -60.0
eduroam : -59.0
UGM-Hotspot : -48.0
c
89.0
c(3.0,3.0)
UGM-Secure : -60.0
eduroam : -59.0
UGM-Hotspot : -40.0
e
94.0
e(5.0,5.0)
UGM-Secure : -65.0
eduroam : -66.0
UGM-Hotspot : -39.0
如果第一个最接近的值是 f 值 17,那么第二个最接近的值是 b 还是 d 对吗?但是为什么它返回具有最远值的 e 呢?实际上,我也在寻找第三个最接近的值,如果还有办法找到第三个最接近的值,那就太好了。
编辑:这是我的 PositionData 类
public class PositionData implements Serializable {
public static final int MAX_DISTANCE=99999999;
private String name;
double x;
double y;
public static final int MINIMUM_COMMON_ROUTERS=1;
public HashMap<String, Double> values;
public HashMap<String,String> routers;
public PositionData(String name, double x, double y) {
// TODO Auto-generated constructor stub
this.name=name;
this.x=x;
this.y=y;
values = new HashMap<String, Double>();
routers = new HashMap<String, String>();
}
public void addValue(Router router, double strength){
values.put(router.getBSSID(), strength);
routers.put(router.getBSSID(),router.getSSID());
}
public String getName() {
return name;
}
public Double getX() {
return x;
}
public Double getY() {
return y;
}
public String toString() {
String result="";
result+=name+"("+x+","+y+")"+"\n";
for(Map.Entry<String, Double> e: this.values.entrySet())
result+=routers.get(e.getKey())+" : "+e.getValue().toString()+"\n";
return result;
}
public HashMap<String, Double> getValues() {
return values;
}
public double uDistance(PositionData arg,ArrayList<Router> friendlyWifis){
double sum=0;
double count=0;
for(Map.Entry<String, Double> e: this.values.entrySet()){
Double v;
//Log.v("Key : ",arg.values.get(e.getKey()).toString());
if(isFriendlyWifi(friendlyWifis,e.getKey()) && arg.values.containsKey(e.getKey()))
{
v=arg.values.get(e.getKey());
sum+=Math.pow((v-e.getValue()),2);
count++;
}
}
if(count<MINIMUM_COMMON_ROUTERS){
sum=MAX_DISTANCE;
}
return sum;
}
private boolean isFriendlyWifi(ArrayList<Router> wifis, String bssid){
for(int i=0;i<wifis.size();i++){
if(wifis.get(i).getBSSID().equals(bssid))
return true;
}
return false;
}
}
【问题讨论】:
标签: java android if-statement coordinates closest