如何比较另一个类的两个对象?

时间:2015-01-19 05:19:37

标签: java

我必须编写一个方法,它将2个对象P1和P2作为参数(CP类的对象) 返回-1是P1小于P2,如果P1大于P2,则返回1;如果P1 == P2

,则返回0

我的主要代码是

public class PostCodeSorter 
{
    public void sortPostcodeString(String p1, String p2)
    {
        String area1=null,area2=null;
        int regionCode1=0,regionCode2=0;
        String subRegionCode1=null,subRegionCode2=null;
        int sectorCode1=0,sectorCode2=0;
        String unit1=null,unit2=null;

        CPW_PostCode P1=new CPW_PostCode();
        CPW_PostCode P2=new CPW_PostCode();

        String[] PostCodeSepeat_p1 = postcodeSeperator(p1);

        area1 = PostCodeSepeat_p1[0];
        if(PostCodeSepeat_p1[1]!=null) 
            regionCode1 = Integer.parseInt(PostCodeSepeat_p1[1]);
        else 
            regionCode1=0;
        subRegionCode1 = PostCodeSepeat_p1[2];
        if(PostCodeSepeat_p1[3]!=null) 
            sectorCode1 = Integer.parseInt(PostCodeSepeat_p1[3]);
        else
            sectorCode1=0;
        unit1 = PostCodeSepeat_p1[4];

        P1.setArea(area1);
        P1.setRegionCode(regionCode1);
        P1.setSubRegionCode(subRegionCode1);
        P1.setSectorCode(sectorCode1);
        P1.setUnit(unit1);


        String[] PostCodeSepeat_p2 = postcodeSeperator(p2);

        area2 = PostCodeSepeat_p2[0];
        if(PostCodeSepeat_p2[1]!=null) 
            regionCode2 = Integer.parseInt(PostCodeSepeat_p2[1]);
        else 
            regionCode2=0;
        subRegionCode2 = PostCodeSepeat_p2[2];
        if(PostCodeSepeat_p2[3]!=null) 
            sectorCode2 = Integer.parseInt(PostCodeSepeat_p2[3]);
        else
            sectorCode2=0;
        unit2 = PostCodeSepeat_p2[4];

        P2.setArea(area2);
        P2.setRegionCode(regionCode2);
        P2.setSubRegionCode(subRegionCode2);
        P2.setSectorCode(sectorCode2);
        P2.setUnit(unit2);

        // Afshan - write a procedure which will take P1 and P2 as arguments and return -1 is P1 is smaller than P2, 1 if P1 is greater than P2 and 0 if P1 == P2

        result = comparePostalCode(P1,P1);
        //System.out.println("P1 is:"+P1);
        System.out.println("RESULAT IS:"+result);
        System.out.println("Area1            : " + area1);
        System.out.println("regionCode1      : " + regionCode1);
        System.out.println("subRegionCode1   : " + subRegionCode1);
        System.out.println("sectorCode1      : " + sectorCode1);
        System.out.println("unit1            : " + unit1);

        System.out.println("Area2            : " + area2);
        System.out.println("regionCode2      : " + regionCode2);
        System.out.println("subRegionCode2   : " + subRegionCode2);
        System.out.println("sectorCode2      : " + sectorCode2);
        System.out.println("unit2            : " + unit2);

    }
public static void main(String[] args) 
    {
        try 
        {
                PostCodeSorter p = new PostCodeSorter();
        //p.Sort();
        p.sortPostcodeString("M1 1AA","EC1A 1BB");
    } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

CP计划

public class CP 
{
    static String area;
    int regionCode;
    String subRegionCode;
    int sectorCode;
    String unit;
    public String getArea()
    {
        return area;
    }
    public void setArea(String area)
    {
        this.area = area;
    }
    public int getRegionCode() {
        return regionCode;
    }
    public void setRegionCode(int regionCode)
    {
        this.regionCode = regionCode;
    }
    public String getSubRegionCode()
    {
        return subRegionCode;
    }
    public void setSubRegionCode(String subRegionCode)
    {
        this.subRegionCode = subRegionCode;
    }
    public int getSectorCode() 
    {
        return sectorCode;
    }
    public void setSectorCode(int sectorCode)
    {
        this.sectorCode = sectorCode;
    }
    public String getUnit()
    {
        return unit;
    }
    public void setUnit(String unit) 
    {
        this.unit = unit;
    }

}

很困惑..任何人都可以帮助我...... !!!

3 个答案:

答案 0 :(得分:3)

您需要根据某些参数集比较两个对象,可以是一个或多个。

Java API确实提供了这样的接口ComparatorComparable

请参阅示例herehere

答案 1 :(得分:1)

您想要比较2个对象。 这就是为什么不可能进行自然排序的原因。 您可以根据提到的unitId。

对某些属性进行排序

Java collection API已经有了Comperable和Comperator接口。两者都有方法compareTo和compare分别根据您的要求为您提供输出

答案 2 :(得分:1)

我的GUESS:  根据我在线上的启示< result = comparePostalCode(P1,P1);'您的代码,您可以使用邮政(ZIP)代码进行比较。

' regionCode'和' sectorCode'可能代表美国邮政编码的前5位数字和4位数字。

在这个解释中,白宫(邮编:20500)小于夏威夷威基基的一家酒店(邮编:96815)。

比较CP类的两个对象的代码如下:

public int comparePostalCode(CP p1, CP p2)
{
    int reg1,sec1,reg2,sec2; /* region codes and sector codes */

    /* extract region code from arguments */
    reg1 = p1.getRegionCode();
    reg2 = p2.getRegionCode();
    if (reg1<reg2)
        return -1; /* p1 is smaller than p2 by region code*/
    else if (reg1>reg2)
        return 1; /* p1 is larger than p2 by region code*/
    else
    {
        sec1=p1.getSectorCode();
        sec2=p2.getSectorCode();
        if (sec1<sec2)
            return -1; /* p1 is smaller than p2 by sector code*/
        else if (sec1>sec2)
            return 1; /* p1 is larger than p2 by sector code*/
        else
            return 0;
    }
}