Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在数组中循环查找最近的距离?_Java_Loops_Distance - Fatal编程技术网

Java 如何在数组中循环查找最近的距离?

Java 如何在数组中循环查找最近的距离?,java,loops,distance,Java,Loops,Distance,我一直在试图找出如何循环遍历一系列邮政编码,并找到与目标最近的邮政编码。邮政编码还包含纬度和经度,用于计算到目标的距离。我需要弄清楚如何在存储最近距离的数组中循环,然后返回最近距离的邮政编码。任何帮助都会很好,因为我已经尝试了我能想到的一切 * Finds and returns the zip code of a postal zone in the collection * whose centroid is closest to a given location. * * @p

我一直在试图找出如何循环遍历一系列邮政编码,并找到与目标最近的邮政编码。邮政编码还包含纬度和经度,用于计算到目标的距离。我需要弄清楚如何在存储最近距离的数组中循环,然后返回最近距离的邮政编码。任何帮助都会很好,因为我已经尝试了我能想到的一切

 * Finds and returns the zip code of a postal zone in the collection
 * whose centroid is closest to a given location. 
 * 
 * @param   target    the target location.
 * 
 * @return  returns zipCode of the postal zone whose centroid is closest;
 *          returns COLLECTION_EMPTY if no zones are in the collection.
 */
public String findClosestZone(Location target)
{
    int counter = 0;
    String closeZip = COLLECTION_EMPTY;
    double closestDistance = 100.0;
    for (int i = 0; i < this.zoneCount; i++)
    {
        if (this.zones[i].getZoneLocation()
            .calcDistance(target) < closestDistance)
        {
            closeZip = this.zones[i].getZoneZipCode();
            closestDistance = this.zones[i]
            .getZoneLocation().calcDistance(target);
            counter++;
            return closeZip;
        }
    }
    return closeZip;
}    
根据:

这意味着您的代码在第一次迭代后完成其工作。据我所知,你想在一系列区域中找到最近的一个。 我想你不需要在循环内返回。请评论或删除它

public String findClosestZone(Location target)
    {
        int counter = 0;
        String closeZip = COLLECTION_EMPTY;
        double closestDistance = 100.0;
        for (int i = 0; i < this.zoneCount; i++)
        {
            if (this.zones[i].getZoneLocation().calcDistance(target) < closestDistance)
            {
                closeZip = this.zones[i].getZoneZipCode();
                closestDistance = this.zones[i].getZoneLocation().calcDistance(target);
                counter++;
                // return closeZip;
            }
        }
        return closeZip;
    }

欢迎来到SO。来,坐火车。这里没有问题……返回是为了返回距离目标位置最近的邮政编码。事实上,它返回的不是最近的邮政编码,而是第一个邮政编码。你需要在数组中循环并找到最近的一个?对吗?是的,这就是我想要完成的。好的,你能不能在没有内在回报的情况下尝试一下?当然。哇,成功了。我不明白为什么,但那很好。谢谢
public String findClosestZone(Location target)
    {
        int counter = 0;
        String closeZip = COLLECTION_EMPTY;
        double closestDistance = 100.0;
        for (int i = 0; i < this.zoneCount; i++)
        {
            if (this.zones[i].getZoneLocation().calcDistance(target) < closestDistance)
            {
                closeZip = this.zones[i].getZoneZipCode();
                closestDistance = this.zones[i].getZoneLocation().calcDistance(target);
                counter++;
                // return closeZip;
            }
        }
        return closeZip;
    }