2017-04-13 88 views
0

對於我當前的項目,我必須搜索ZipCode對象的ArrayList以便從用戶輸入的int zip中找到最遠的ZipCode。搜索對象的ArrayList

以下是編寫我遇到麻煩的方法的說明: public ZipCode findFurthest(int pZip) - 找到離提供的郵政編碼最遠的ZipCode。如果未找到郵政編碼,則返回null。例如,離郵政編碼75234最遠的是ADAK,AK 99546.

在我的代碼中,我使用公共int距離(int zip1,int zip2)來計算用戶輸入的zip與郵編之間的距離的ArrayList對象。我不知道如何編寫正確的if語句,以便找到距離最遠的ZipCode。 謝謝你的幫助,我非常感謝你。

public class ZipCodeDatabase { 

    private ArrayList<ZipCode> list; 

    /** 
    * Constructor for objects of class ZipCodeDatabase 
    */ 
    public ZipCodeDatabase() 
    { 
     // initialise instance variables 
     list = new ArrayList<ZipCode>(); 
    } 

    /** 
    * Method findZip searches loops through the ArrayList and returns all of the ZipCodes 
    * 
    * @param int zip 
    * @return  null 
    */ 
    public ZipCode findZip(int zip) 
    { 
     // put your code here 

     for(ZipCode z : list){ 
      if(z.getZip() == zip){ 
       return z; 
      } 

     } 
     return null; 
    } 

    /** 
    * Method distance calculates the distance between two user entered zip codes numbers 
    * 
    * @param int zip1, int zip2 
    * @return int 
    */ 
    public int distance(int zip1, int zip2){ 

     ZipCode z1 = new ZipCode(zip1); 
     ZipCode z2 = new ZipCode(zip2); 
     z1 = findZip(zip1); 
     z2 = findZip(zip2); 

     double lat1 = z1.getLat(); 
     double lat2 = z2.getLat(); 
     double lon1 = z1.getLon(); 
     double lon2 = z2.getLon(); 
     final int EARTH_RADIUS = 3959; 
     if(list.contains(z1) && list.contains(z2)){ 
      double p1= Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lon1)) 
       * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(lon2)); 

      double p2 = Math.cos(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lon1)) 
       * Math.cos(Math.toRadians(lat2)) * Math.sin(Math.toRadians(lon2)); 

      double p3 = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)); 

      double distance = Math.acos(p1 + p2 + p3) * EARTH_RADIUS; 

      double d = distance; 

      int dist = (int) d; 

      return dist; 
     } 
     else{ 
      return -1; 
     } 

    } 

    /** 
    * Method withinRadius finds all of the ZipCode objects within the radius of the entered 
    * zip 
    * @param int pZip, int pRadius 
    * @return list 
    */ 
    public ArrayList<ZipCode> whithinRadius(int pZip, int pRadius){ 

     for(ZipCode z: list){ 
      if(distance(pZip, z.getZip()) <= pRadius){ 
       ArrayList<ZipCode> radius = new ArrayList<>(); 
       radius.add(z); 
       return radius; 
      } 
      else{ 
       ArrayList<ZipCode> radius = new ArrayList<>(); 
      } 
     } 
     return list; 
    } 

    /** 
    * Method findFurthest finds the furthest ZipCode from the user entered zip 
    * 
    * @param int pZip 
    * @return null 
    */ 
    public ZipCode findFurthest(int pZip){ 

     if(list.contains(pZip)){ 

      for(ZipCode z : list){ 
       if(distance(pZip, z.getZip()) < distance(pZip, z.getZip())){ 
        return z; 
       } 

      } 

     } 
     return null; 
    } 

    /** 
    * Method search find all of the ZipCode objects with a city name that contains the user 
    * entered string 
    * @param String str 
    * @return list 
    */ 
    public ArrayList<ZipCode> search(String str){ 
     ArrayList<ZipCode> matchStr = new ArrayList<>(); 
     for(ZipCode z : list){ 
      if(z.getCity().contains(str)){ 

       matchStr.add(z); 
       return matchStr; 
      } 
      else{ 

       return matchStr; 
      } 

     } 
     return list; 
    } 

    /** 
    * Method readZipCodeDatabase reads the file containing all of the zip codes 
    * 
    * @param String filename 
    * @return int 
    */ 
    public void readZipCodeData(String filename){ 

     Scanner inFS = null; 
     FileInputStream fileByteStream = null; 
     try{ 
      //open the file and set delimeters 
      fileByteStream = new FileInputStream(filename); 
      inFS = new Scanner(fileByteStream); 
      inFS.useDelimiter("[,\r\n]+"); 
      filename = "zipcodes.txt"; 
      // continue while there is more data to read 
      while(inFS.hasNext()) { 

       // read five data elements 
       int zip = inFS.nextInt(); 
       String city = inFS.next(); 
       String state = inFS.next(); 
       double lat = inFS.nextDouble(); 
       double lon = inFS.nextDouble(); 
       ZipCode z = new ZipCode(zip, city, state, lat, lon); 
       ArrayList<ZipCode> list = new ArrayList<>(); 
       list.add(z); 
      } 
      fileByteStream.close(); 
      //error while reading the file 
     }catch(IOException error1){ 
      System.out.println("Error: Unable to read file: " + filename); 
     } 
    } 
} 
+0

您的findFurthest方法將比較表達式兩邊的相同計算距離if(distance(pZip,z.getZip()) Jason

回答

0

該段應做的伎倆:

public ZipCode findFurthest(int pZip) 
{ 
    return list.stream() 
     .max((zip0, zip1) -> distance(pZip, zip0.getZip()) 
      - distance(pZip, zip1.getZip())) 
     .orElse(null); 
} 

沒什麼花哨的 - 只是一個普通的最小 - 最大代碼。請寫測試來檢查這個方法是否真的有效。