搜索对象的ArrayList

时间:2017-04-13 00:14:01

标签: java search arraylist

对于我当前的项目,我必须搜索ZipCode对象的ArrayList,以便从用户输入的int zip中找到最远的ZipCode。

以下是编写我遇到问题的方法的说明: public ZipCode findFurthest(int pZip) - 找到距离提供的邮政编码最远的ZipCode。如果找不到邮政编码,则返回null。例如,距离邮政编码75234最远的地方是ADAK,AK 99546。

在我的代码中,我使用public int distance(int zip1,int zip2)方法计算用户输入的zip与ArrayList对象的zip之间的距离。我不确定如何编写正确的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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这个片段应该可以解决问题:

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

没什么特别的 - 只是一个常规的最小 - 最大代码。请编写测试以检查此方法是否真的有效。