【问题标题】:Issues utilizing the comparable interface使用类似界面的问题
【发布时间】:2013-11-08 05:55:41
【问题描述】:

我正在尝试利用可比较的接口对数组进行排序。 compareTo() 方法是这样编写的,因此钻石首先按胡萝卜排序,然后按净度或颜色排序,以对特定钻石更好的为准。由于有 23 个颜色等级,但净度只有 11 个等级,所以将前两个颜色等级视为与一级净度等级相同,后两个颜色等级与二级净度等级相同,以此类推.我不确定如何将颜色与清晰度进行比较,因为一个是字符串,另一个是字符。这是我到目前为止所拥有的。

public Diamond(String sN, double car, String clar, char col, String cutType)
{
    stockNumber = sN; 
    carot = car; 
    clarity = clar; 
    color = col; 
    cut = cutType;

}

public int compareTo(Diamond other)
{
    if (getCarot() < other.getCarot())
    {
        return 1;
    }
    else if(getCarot() > other.getCarot())
    {       
        return -1;
    }
    else return 0; 
}
public String toString()
{
    return "{stockNumber :: " +getStock() + " carot :: " +getCarot() + " clarity :: " +getClarity()+ " color :: " +getColor() + " cut :: " +getCut()+"}";
    }


//gets the stock number of the diamond
public String getStock()
{
    return stockNumber; 
}

//gets the carot size of the diamond
public double getCarot()
{
    return carot;
}

//gets the clarity of the diamond
public String getClarity()
{
    return clarity; 
}

//gets the color of the diamond
public char getColor()
{
    return color;
}

//gets the cut of the diamond 
public String getCut()
{
    return cut; 
}

Diamond[] 石头 = 新 Diamond[16];

stones[0] = new Diamond( "A1023", 1.0, "VS1",  'F', "brilliant");
stones[1] = new Diamond( "A5911", 1.1, "VVS2", 'G', "rose");
stones[2] = new Diamond( "C5427", 1.0, "VS1",  'D', "princess");
stones[3] = new Diamond( "D8307", 1.6, "SI1",  'H', "brilliant");
stones[4] = new Diamond( "B4825", 0.3, "I1",   'D', "rose");
stones[5] = new Diamond( "A1844", 2.1, "VS2",  'D', "lozenge");
stones[6] = new Diamond( "A3747", 3.1, "SI2",  'W', "baguette");
stones[7] = new Diamond( "E6393", 2.3, "VS2",  'I', "brilliant");
stones[8] = new Diamond( "C5619", 2.8, "VVS1", 'E', "pear");
stones[9] = new Diamond( "E8348", 1.4, "VS2",  'G', "brilliant");
stones[10] = new Diamond( "D2381", 1.7, "I3",   'G', "brilliant");
stones[11] = new Diamond( "C9253", 1.3, "VS2",  'H', "baguette");
stones[12] = new Diamond( "G3459", 2.1, "VS2",  'H', "rose");
stones[13] = new Diamond( "B3598", 2.4, "VVS2", 'D', "pear");
stones[14] = new Diamond( "D9836", 2.8, "IF",   'E', "princess");
stones[15] = new Diamond( "E1046", 2.2, "FL",   'E', "rose");

Arrays.sort( stones );

for ( int j=0; j<stones.length; j++ )
  System.out.println( stones[j].toString() );

}

}

【问题讨论】:

    标签: java interface compare comparable


    【解决方案1】:

    在与多个参数进行比较时,我们应该保持最后返回 0 并继续比较优先级的参数。下面是对胡萝卜、颜色和净度进行排序的代码。

    您可以根据需要在颜色和净度之间添加更多条件。

    public int compareTo(Crawford_Diamond other) {
            if (getCarot() < other.getCarot()) {
                return 1;
            } else if (getCarot() > other.getCarot()) {
                return -1;
            }
    
            if (this.color < other.getColor()) {
                return 1;
            } else if (this.color > other.getColor()) {
                return -1;
            }
    
            if (this.clarity.compareTo(other.getClarity())<1) {
                return 1;
            } else if (this.clarity.compareTo(other.getClarity())>1) {
                return -1;
            }
            return 0;
        }
    

    【讨论】:

    • 我遇到的问题是试图将作为字符串的清晰度与作为字符的颜色进行比较。我需要定义净度“FL”等于颜色“D”或“E”,净度“IF”等于颜色“F”或“G”等......因为有 23 种颜色类型,只有 11 种净度类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2020-06-20
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    相关资源
    最近更新 更多