-3

郵便番号の配列をループして、ターゲットに最も近い郵便番号を見つける方法を見つけようとして立ち往生しています。郵便番号には、ターゲットまでの距離を計算するために使用される緯度と経度も含まれています。最も近い距離を格納する配列をループしてから、最も近い距離の郵便番号を返す方法を理解する必要があります。考えられることはすべて試したので、どんな助けも素晴らしいでしょう。

 * 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;
}    
4

1 に答える 1

2

ドキュメントによると:

A method returns to the code that invoked it when it

1. completes all the statements in the method,
2. reaches a return statement, or
3. throws an exception,
whichever occurs first.

これは、コードが最初の繰り返しの後に作業を終了することを意味します。私が理解している限り、ゾーンの配列の中から最も近いものを見つけたいと思っています。return内部ループは必要ないと思います。コメントするか削除してください。

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;
    }
于 2013-07-20T20:47:14.297 に答える