【问题标题】:Getting an (int,int) parameter to accept a (int, array) input获取 (int,int) 参数以接受 (int, array) 输入
【发布时间】:2015-11-12 01:25:30
【问题描述】:

有什么方法可以将我的 ArrayList myZips 中的 ZipCode 转换为 Integer,以便它可以作为我的距离方法的参数?

邮编类:

public class ZipCode
{
    private int zipCode;
    private String city;
    private String state;
    private double longitude;
    private double latitude;

    public ZipCode(int pZip)
    {
        zipCode   = pZip;
        city      = "UNKOWN";
        state     = "ST";
        latitude  = 0.0;
        longitude = 0.0;
    }

    public ZipCode(int pZip, String pCity, String pState, double pLat, double pLon)
    {
        zipCode   = pZip;
        city      = pCity;
        state     = pState;
        latitude  = pLat;
        longitude = pLon;

    }

    public void setZipCode(int zipCode) {
        this.zipCode = zipCode;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public int getZipCode() {
        return zipCode;
    }

    public String getCity() {
        return city;
    }

    public String getState() {
    return state;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
    public String toString() {
        return city + ", " + state + zipCode;
    }
}


public class ZipCodeDatabase {

    private ArrayList<ZipCode> myZips;

public ZipCodeDatabase()
{
    myZips = new ArrayList<ZipCode>();
}

public ZipCode findZip(int zip){
    for(ZipCode zipCode : myZips){

        if(zipCode.getZipCode() == zip){
            return zipCode;
        }
    }

    return null;

}

public double distance(int zip1, int zip2) {
    int EARTH_RADIUS = 3959;
    if (findZip(zip1) != null && findZip(zip2) != null) {
        ZipCode z1 = new ZipCode(zip1);
        ZipCode z2 = new ZipCode(zip2);
        double lat1  = Math.toRadians(z1.getLatitude());
        double long1 = Math.toRadians(z1.getLongitude());
        double lat2  = Math.toRadians(z2.getLatitude());
        double long2 = Math.toRadians(z2.getLongitude());

        double p1 = Math.cos(lat1) * Math.cos(long1) * Math.cos(lat2) * Math.cos(long2);       
        double p2 = Math.cos(lat1) * Math.sin(long1) * Math.cos(lat2) * Math.sin(long2);         
        double p3 = Math.sin(lat1) * Math.sin(lat2);         
        double distance = Math.acos(p1+p2+p3) * EARTH_RADIUS;
        int distance1 = (int)Math.round(distance);
        return distance1;
    }
    else {
        return -1;
    }
}
public ArrayList <ZipCode> withinRadius(int pZip, int pRadius){
    ArrayList<ZipCode> zips = new ArrayList<ZipCode> ();
    if (findZip(pZip) != null){
        for (int i = 0; i < myZips.size(); i++){
            if (distance(pZip, myZips.get(i)) <= pRadius) //the trouble is here, how can I convert the myZips.get(I) into the relevant 5 digit integer .
            zips.add(pZip);
        }
    }
    return zips;
}

}

我知道这是一个非常专业和基本的问题,但我是 Java 新手,似乎无法弄清楚。

【问题讨论】:

  • 可能是Integer.parseInt(myZips[i])?
  • ZipCode 类是什么样的?
  • 我会坚持使用 ZipCode 并将 int 参数更改为 ZipCode。 Ints 可能在您居住的地方工作,但在这里,我们在邮政编码中既有字母也有数字。
  • 它说它需要一个数组,但找到了一个 ArrayList。
  • findZip 不只是在做myZips.contains(zip) 吗?除了返回对象而不是 true/false。

标签: java


【解决方案1】:

我写了findZip method! :)

如果你只有两个拉链可以看的话,我猜你只需要这个。

ZipCode zip1 = myZips.get(0);
ZipCode zip2 = myZips.get(1);
double d = distance(zip1.getZipCode(), zip2.getZipCode());

如果您想转换整个 ZipCode 列表,您可以这样做

List<Integer> zipCodeInts = new ArrayList<>(myZips.size());
for(ZipCode zipCode : myZips){
    zipCodeInts.add(zipCode.getZipCode());
}
// now use zipCodeInts somehow

旁注,您可以保存这两个new。而不是这样做:

if (findZip(zip1) != null && findZip(zip2) != null) {
    ZipCode z1 = new ZipCode(zip1);
    ZipCode z2 = new ZipCode(zip2);
    // do stuff
}

你可以这样做

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

if(z1 != null && z2 != null) {
    // do stuff
}

【讨论】:

  • 很高兴知道您的代码被用于某事:D
  • @jb 是的。我会确保我不会在我的最终版本中使用它。
【解决方案2】:

我不明白拥有findZip 背后的原因,因为我认为您进行的查找比您需要的要多,但无论如何,您已经接近了,只需要保存对您找到的ZipCode 的引用findZip 并在 distance 方法中调用 getZipCode()

public static final int EARTH_RADIUS = 3959; // Move constants out of methods

public ZipCode findZip(int zip){
    for(ZipCode zipCode : myZips){
        if(zipCode.getZipCode() == zip){
            return zipCode;
        }
    }
    return null;
}

public int distance(int zip1, int zip2){
    // save these references so you can use them later
    ZipCode z1 = findZip(zip1);
    ZipCode z2 = findZip(zip2);

    if(z1 != null && z2 != null) {
        double lat1  = Math.toRadians(z1.getLatitude());
        double long1 = Math.toRadians(z1.getLongitude());
        double lat2  = Math.toRadians(z2.getLatitude());
        double long2 = Math.toRadians(z2.getLongitude());

        double p1 = Math.cos(lat1) * Math.cos(long1) * Math.cos(lat2) * Math.cos(long2);       
        double p2 = Math.cos(lat1) * Math.sin(long1) * Math.cos(lat2) * Math.sin(long2);         
        double p3 = Math.sin(lat1) * Math.sin(lat2);         

        double distance = Math.acos(p1+p2+p3) * EARTH_RADIUS;
        return (int)Math.round(distance);
    }
    else return -1;
}

public ArrayList <ZipCode> withinRadius(int pZip, int pRadius){
    ZipCode zip = findZip(pZip); // save this reference to calculate the distances

    ArrayList<ZipCode> zips = new ArrayList<ZipCode> ();
    if (zip != null) {
        for (int i = 0; i < myZips.size(); i++) {
            ZipCode z = myZips.get(i); // or use a for-each loop like in findZip
            if (distance(zip.getZipCode(), z.getZipCode()) <= pRadius) {
                zips.add(z);
            }
        }
    }
    return zips;
}

【讨论】:

  • 我不允许使用方法参数(出于某种可怕的原因)。我之前也尝试过类似的方法,但它给了我一个静态上下文错误。
  • @David - 如果只给定整数,你应该如何在distance 方法中获取经度和纬度?使用findZip?
  • 这些是给我的说明: public int distance(int zip1, int zip2) - 返回两个邮政编码之间的距离(以英里为单位)。请参阅本文档末尾的信息以计算距离。调用您的 findZip() 方法,而不是在此方法中搜索。如果其中一个邮政编码不存在,则返回 -1。请注意,您需要在返回之前将解决方案转换为整数。
  • 后面的信息只是方法底部的方程式
  • @David,指令说public int distance(...),你的代码有public double distance(...)
【解决方案3】:

只需获取包含班级邮政编码的int

ZipCodeDatabase.distance(myZipCode.getZipCode(), yourZipCode.getZipCode());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2016-06-10
    • 2013-05-20
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    相关资源
    最近更新 更多