Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何比较另一类的两个对象?_Java - Fatal编程技术网

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

Java 如何比较另一类的两个对象?,java,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;

我必须编写一个方法,将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;
    }

}

我很困惑。。任何人都可以帮我

您需要根据特定的参数集比较两个对象,可以是一个或多个

JavaAPI确实提供了这样的接口和


请参考示例和

您想要比较两个对象。 这就是为什么自然排序是不可能的。 您可以根据您提到的unitId的某些属性进行排序

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

我猜: 根据我在“result=comparePostalCodeP1,P1;”行中的输入对于您的代码,您可以使用邮政编码进行比较

“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;
    }
}

判断CP的一个实例是否大于另一个实例的标准是什么?按区域?我必须打印小的。没有。。公共静态int comparePostalCodeCPW_邮政编码P1,CPW_邮政编码P2{System.out.PrintLUnit:+P1.getUnit.compareToP2.getUnit;}Am字符串类似于此,区域代码和扇区代码都可以。但对于面积??它应该检查所有内容@FumuI不知道“区域”、“分区代码”和“单位”,因为你没有解释它们!您还没有解释如何比较两个数据。由于缺乏信息,我无法回答,所以我猜测。