比较两个对象,如果有的话,调用什么方法

时间:2017-11-02 22:22:19

标签: java object arraylist

我想在public int compareTo(Object other)中比较两个DNumber对象 我以为我可以像上面的equals方法那样做类似的东西,但我不能像这样调用Decimal

if(toDecimal() == other.toDecimal()){
        return 0;
    }

但这是不可能的。我的下一步应该是什么?还有另一种方法来检查它们是否相等?我应该使用其他方法。

public class DNumber
{
// instance variables - replace the example below with your own

ArrayList<Digit> binary = new ArrayList<Digit>();
/**
 * Constructor for objects of class DNumber
 */
public DNumber()
{
    Digit num = new Digit(0);
    binary.add(num);
}
public DNumber(int val){
    int num = val;
    if(num > 0){
        while (num > 0){
            Digit bin = new Digit(num%2);
            num /= 2;
            binary.add(0,bin);
        }    
    }
    else{
        Digit bin = new Digit(0);
        binary.add(0,bin);
    }
}
/**
 * An example of a method - replace this comment with your own
 *
 * @param  y  a sample parameter for a method
 * @return    the sum of x and y
 */
public String toString(){
    String s = "";
    for(Digit d : binary){
        s = s + d.toString();
    }
    return s;
}
public void add(DNumber b){
    int ArraySize1 = binary.size();
    int ArraySize2 = b.binary.size();
    int difference = 0;
    int i = 0;
    Digit zero = new Digit(0);
    //Checks to see the difference in arrayLists to make them equal
    if(ArraySize1 > ArraySize2){
        difference = ArraySize1 - ArraySize2;
        while(difference > i){
            b.binary.add(zero);
            ++i;
        }

    }
    else if(ArraySize1 < ArraySize2){
        difference = ArraySize2 - ArraySize1;
        while(difference > i){
            System.out.println(b.binary);
            binary.add(zero);
            ++i;
        }
    }
    else if(ArraySize1 == ArraySize2){
        difference = 0;
    }
    //System.out.println(binary + " " + "   " + b.binary);



    //To get the last value of arrays.
    Digit carry = new Digit(0);
    int j = binary.size()-1;
    while(j >= 0){
        Digit bin1 = binary.get(j);
        Digit bin2 = b.binary.get(j);
        Digit sum = bin1.add(bin2);
        System.out.println(sum);
        j--;

    }


}
public int toDecimal(){
    //Iterates through arraylist binary, creates a string, parses string to create result in decimal
    String initial = "";
    for(Digit d: binary){
    initial = initial + d.toString();  
    }
    int result = Integer.parseInt(initial, 2);
    return result;

}
//Checks to see if objects are equal.
public boolean equals(Object e){
   if( toString().equals(e.toString())){
       return true;
    }
    return false;
}
//Returns 0 if this DNumber equals other, a negative result if this DNumber is less than other, 
// and a positive result otherwise.  You can assume that other is a DNumber
public int compareTo(Object other){


}
}

2 个答案:

答案 0 :(得分:1)

安迪特纳是对的。你的DNumber应该实现Comparable。这将要求您实现public int compareTo(DNumber other)。但是,你的compareTo必须与equals()和hashcode()一致,这通常意味着覆盖这些方法。也就是说,两个相等的实例必须返回相同的哈希码,并且compareTo()在相互比较时必须返回0。

答案 1 :(得分:0)

您的compareTo方法没有理由采用Object参数。这样做意味着您无法在参数上调用toDecimal(),因为编译器无法确定它是否只传递DNumber的实例(您可以将其传递给String那么它会怎么做?)。

也许您对equals感到困惑,Object需要使用Object.equals参数才能正确覆盖compareTo;您的DNumber方法没有此类限制,因为您没有覆盖某个方法(但请参见下文)。

将参数类型更改为other.toDecimal(),您就可以调用Collections.sort了。

为了让您的课程更有用(例如,允许List<DNumber>能够对Comparable进行排序),请使类实现class DNumber implements Comparable<DNumber> { // ... @Override public int compareTo(DNumber other) { // ... } }

string = "Create your own function that takes in a sentence and replaces every second word with the word “Hello”"

hello = " ".join(["{} hello".format(word) 
        for idx, word in enumerate(string.split()) 
        if idx % 2 == 0])
print(hello)
# Create hello own hello that hello in hello sentence hello replaces hello second hello with hello word hello