2013-07-20 4 views
-3

나는 우편 번호 배열을 통해 반복하고 대상에 가장 가까운 우편 번호를 찾는 방법을 찾으려고 노력 중이다. 우편 번호는 또한 대상까지의 거리를 계산하는 데 사용되는 위도와 경도를 포함합니다. 가장 가까운 거리를 저장하고 가장 가까운 배열의 우편 번호를 반환하는 배열을 반복하는 방법을 알아 내야합니다. 제가 생각할 수있는 모든 것을 시도했기 때문에 어떤 도움도 좋을 것입니다. doc에 따르면가장 가까운 거리를 찾기 위해 배열을 반복하는 방법은 무엇입니까?

* 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; 
}  
+3

안녕하세요. 여기, [투어] (http://stackoverflow.com/about)를 가져 가라. 여기에 아무런 질문이 없습니다 ... –

답변

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; 
    } 
+0

반환은 대상 위치에 가장 가까운 우편 번호를 반환하는 것입니다. – user1807902

+0

사실 그것은 가장 가까운 것이 아니라 첫 번째 것을 반환합니다. 배열을 반복하고 가장 가까운 배열을 찾아야합니까? 옳은? – shevchyk

+0

예, 그게 내가 성취하려고하는 것입니다. – user1807902

관련 문제