获取(int,int)参数以接受(int,array)输入

时间:2015-11-12 01:25:30

标签: java

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

ZipCode类:

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的新手并不能解决这个问题。

3 个答案:

答案 0 :(得分:5)

我写了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
}

答案 1 :(得分:0)

只需获取包含该类邮政编码的int

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

答案 2 :(得分:0)

我不理解findZip背后的原因,因为我认为你做的比你需要的更多,但是无论如何,你很接近,只需要保存对{{1}的引用您从ZipCode找到并在findZip方法中调用getZipCode()

distance