简体   繁体   English

Java从for循环中找到最小值

[英]Java finding minimum value from for loop

I was having some problem when trying to find the minimum distance and from the minimum distance, I then plot two circles onto the map.我在尝试找到最小距离和最小距离时遇到了一些问题,然后我在地图上绘制了两个圆圈。

                    double distance = 0;
                    double minDistance;
                    convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
                    for(int i = 0; i < convertedHotSpotGeomList.size(); i++){
                        LatLng point1 = new LatLng(convertedHotSpotGeomList.get(i).getY(), convertedHotSpotGeomList.get(i).getX());                                 
                        LatLng point2 = new LatLng(convertedHotSpotGeomList.get(++i).getY(), convertedHotSpotGeomList.get(++i).getX());
                        distance = calculateHotSpot(point1, point2);
                        Log.i("DIST", String.valueOf(distance));
                        minDistance = distance;
                        if(minDistance < distance){
                            minDistance = distance;
                            Log.i("MIN", String.valueOf(minDistance));
                             CircleOptions circleOptions = new CircleOptions()
                                .center(point1)
                                .radius(1000)
                                .fillColor(Color.argb(95, 178, 30, 37))
                                .strokeColor(Color.TRANSPARENT);

                                googleBasemap.addCircle(circleOptions);

                                CircleOptions circleOptions1= new CircleOptions()
                                .center(point2)
                                .radius(1000)
                                .fillColor(Color.argb(95, 88, 130, 37))
                                .strokeColor(Color.TRANSPARENT);

                                googleBasemap.addCircle(circleOptions1);

                        }
                    }

Currently I am getting 15, 3, 15, 14 as DIST but I could not get the minDist as it is not printed out.目前我得到 15, 3, 15, 14 作为 DIST 但我无法得到 minDist 因为它没有打印出来。 Any ideas?有任何想法吗?

Thanks in advance.提前致谢。

You probably want:你可能想要:

    distance = calculateHotSpot(point1, point2);
    ....
    if(distance < minDistance){  
        minDistance = distance;

and initialize minDistance to Double.MAX_VALUE : double minDistance = Double.MAX_VALUE并将minDistance初始化为Double.MAX_VALUEdouble minDistance = Double.MAX_VALUE

The original few lines does not make much sense, because minDistance < distance would never evaluates to true since you just set minDistance = distance;最初的几行没有多大意义,因为minDistance < distance永远不会评估为 true,因为您只是设置了minDistance = distance;

    distance = calculateHotSpot(point1, point2);
    ....
    minDistance = distance;
    if(minDistance < distance){ // will never happen
        minDistance = distance;

Updated:更新:

More complete solution as the point above is not the only issue:更完整的解决方案,因为上述点不是唯一的问题:

double minDistance = Double.MAX_VALUE;
convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
LatLng[] minPoints = new LatLng[2]; // save the minimum points
for(int i = 0; i < convertedHotSpotGeomList.size(); i++){
    LatLng point1 = new LatLng(convertedHotSpotGeomList.get(i).getY(),   convertedHotSpotGeomList.get(i).getX());                                 
    LatLng point2 = new LatLng(convertedHotSpotGeomList.get(++i).getY(), convertedHotSpotGeomList.get(++i).getX());
    distance = calculateHotSpot(point1, point2);
    Log.i("DIST", String.valueOf(distance));
    if(distance < minDistance){
        minDistance = distance;
        Log.i("MIN", String.valueOf(minDistance));
        minPoints[0] = point1;
        minPoints[1] = point2;
    }
}
// we finish all the comparison, so we draw the circles now 
if(minPoints[0]!=null && minPoints[1] !=null){
    CircleOptions circleOptions = new CircleOptions()
        .center(minPoints[0])
        .radius(1000)
        .fillColor(Color.argb(95, 178, 30, 37))
        .strokeColor(Color.TRANSPARENT);
    CircleOptions circleOptions1= new CircleOptions()
        .center(minPoints[1])
        .radius(1000)
        .fillColor(Color.argb(95, 88, 130, 37))
        .strokeColor(Color.TRANSPARENT);

    googleBasemap.addCircle(circleOptions);
    googleBasemap.addCircle(circleOptions1);
}

Your problem lies in these two lines next to each other:你的问题在于这两行彼此相邻:

                    minDistance = distance;
                    if(minDistance < distance){

So first you are setting minDistance to the value of distance.所以首先你将 minDistance 设置为距离的值。 Then you are checking to see if minDistance is less than distance.然后您正在检查 minDistance 是否小于距离。 Since it will never be less than the value you just set it to, that block of code will never evaluate.由于它永远不会小于您刚刚为其设置的值,因此该代码块永远不会求值。

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

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