简体   繁体   English

多项式算术Java equals方法

[英]polynomial arithmetic java equals method

I'm working on a polynomial calculator. 我正在使用多项式计算器。 My problem is with the equals method. 我的问题是使用equals方法。 Here is the relevant code: 以下是相关代码:

public class Poly{
    Term[] terms;

    //Constructors-------------------------------------------
    public Poly() {}
    public Poly(ArrayList<Term> Terms) {
        terms = Terms.toArray(new Term[Terms.size()]);
        Arrays.sort(terms, new TermComparator());
    }

    //Methods-------------------------------------------------
    public boolean equals(Poly x) {
        boolean q=false;
        if(this == x){
            q=true;
        }
    return q;
    }

    //used in constructor to order terms
    class TermComparator implements Comparator<Term> {
        @Override
        public int compare(Term t1, Term t2) {
            return t2.getExp() - t1.getExp();
        }
    }
}

The equals method always returns false even when two Poly objects have the same value. 即使两个Poly对象具有相同的值,equals方法也始终返回false。 Can anyone help please? 有人可以帮忙吗?

Your Poly class equals method should be like below 您的Poly类equals方法应如下所示

@Override
public boolean equals(Object obj) {
    if (this == obj) //checking both are same instance
        return true;
    if (obj == null) // checking obj should not be null
        return false;
    if (getClass() != obj.getClass()) //checking both objects from same class
        return false;
    Poly other = (Poly) obj; 

    return Arrays.equals(terms, other.terms);  //checking all the array values
}

if you are adding Poly objects to collection you need to implement hash code method too. 如果要将Poly对象添加到集合中,则也需要实现哈希码方法。

    @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Arrays.hashCode(terms);
    return result;
}   

Please refer 请参考

Why do I need to override the equals and hashCode methods in Java? 为什么需要重写Java中的equals和hashCode方法?

How should equals and hashcode be implemented when using JPA and Hibernate 使用JPA和Hibernate时应如何实现equals和hashcode

It seems you need the following 2 changes: 似乎您需要进行以下2个更改:

  1. Do not compare references using code as follows: 不要使用以下代码比较引用:

     if(this == x){ q=true; } 

    You need to compare the content of the object - the contents of terms in your case. 您需要比较对象的内容-您的案例中terms的内容。

  2. When overriding the equals method, you'd better override the hashcode method as well. 覆盖equals方法时,最好也覆盖hashcode方法。

My solution involved creating an equals method in the term class first. 我的解决方案涉及首先在术语类中创建一个equals方法。 You would then use that equals method to write the equals method in the polynomial class. 然后,您可以使用equals方法在多项式类中编写equals方法。 So here's the code for the equals method for terms: 因此,这是条款的equals方法的代码:

public boolean equals(Term x){
    boolean a= false;
    int expThis = this.getExp();
    int coefThis = this.getCoeff();
    int expX = x.getExp();
    int coefX = x.getCoeff();
    if(expThis==expX && coefThis==coefX){
        a=true;
    }
    return a;
}

My polynomial constructor already organizes all terms in decreasing order. 我的多项式构造函数已经按降序组织了所有项。 If you have polynomials in order then all you have to do is first check that the two polynomials are the same size and then loop through all the terms of the two polynomials, using the equals method from the term class to compare terms. 如果按顺序具有多项式,那么您要做的就是首先检查两个多项式的大小是否相同,然后使用术语类中的equals方法比较两个多项式,遍历两个多项式的所有项。 So here's the code for the equals method for polynomials: 因此,这是多项式的equals方法的代码:

public boolean equals(Object obj) {
    boolean w=false;
    Poly other = (Poly) obj;
    int L1 = other.terms.length;
    int L2 = this.terms.length;
    if(L1==L2){
        for(int q=0; q<L1; q++){
            Term a=other.terms[q];
            Term b=this.terms[q];
            if(a.equals(b)==true){
                w=true;
            }
            else{
                w=false;
                break;
            }
        }
    }
    return w;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM