Java 多项式算法

Java 多项式算法,java,eclipse,Java,Eclipse,我在做多项式计算器。我的问题是使用equals方法。以下是相关代码: public class Poly{ Term[] terms; //Constructors------------------------------------------- public Poly() {} public Poly(ArrayList<Term> Terms) { terms = Terms.toArray(new Term[Terms.si

我在做多项式计算器。我的问题是使用equals方法。以下是相关代码:

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();
        }
    }
}
公共类Poly{
期限[]期限;
//建设者-------------------------------------------
公共多边形(){}
公共多边形(ArrayList术语){
terms=terms.toArray(新术语[terms.size()]);
sort(terms,newtermparator());
}
//方法-------------------------------------------------
公共布尔等于(多边形x){
布尔q=false;
if(this==x){
q=正确;
}
返回q;
}
//在构造函数中用于排序术语
类TermComparator实现Comparator{
@凌驾
公共整数比较(术语t1,术语t2){
返回t2.getExp()-t1.getExp();
}
}
}

即使两个多边形对象具有相同的值,equals方法也始终返回false。有人能帮忙吗?

您的Poly class 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
}
若要将多边形对象添加到集合中,还需要实现哈希代码方法

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


您的Poly class 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
}
若要将多边形对象添加到集合中,还需要实现哈希代码方法

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


您似乎需要进行以下两项更改:

  • 不要使用以下代码比较引用:

    if(this == x){
        q=true;
    }
    
    您需要比较对象的内容-您案例中的
    术语的内容

  • 重写
    equals
    方法时,最好也重写
    hashcode
    方法


  • 您似乎需要进行以下两项更改:

  • 不要使用以下代码比较引用:

    if(this == x){
        q=true;
    }
    
    您需要比较对象的内容-您案例中的
    术语的内容

  • 重写
    equals
    方法时,最好也重写
    hashcode
    方法


  • 我的解决方案是首先在术语类中创建一个equals方法。然后使用equals方法在多项式类中编写equals方法。下面是术语的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;
    }
    
    我的多项式构造函数已经按降序组织了所有项。如果多项式是按顺序排列的,那么您所要做的就是首先检查两个多项式的大小是否相同,然后使用term类中的equals方法比较两个多项式的所有项。这是多项式的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;
    }
    
    公共布尔等于(对象obj){
    布尔w=假;
    Poly-other=(Poly)obj;
    int L1=其他.terms.length;
    int L2=this.terms.length;
    如果(L1==L2){
    
    对于(int q=0;q我的解决方案是首先在term类中创建一个equals方法。然后使用该equals方法在多项式类中编写equals方法。下面是term的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;
    }
    
    我的多项式构造函数已经按降序组织了所有的项。如果多项式是按序排列的,那么你所要做的就是首先检查两个多项式的大小是否相同,然后使用term类中的equals方法来比较这两个多项式的所有项。下面是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;
    }
    
    公共布尔等于(对象obj){
    布尔w=假;
    Poly-other=(Poly)obj;
    int L1=其他.terms.length;
    int L2=this.terms.length;
    如果(L1==L2){
    
    对于(intq=0;qSo)我应该只得到这个和x的指数和系数,然后比较它们吗?这听起来像个好主意吗?我应该只得到这个和x的指数和系数,然后比较它们吗?这听起来像个好主意。你的equals方法有错吗?我得到返回false在{对于最后一个if语句,但是底部的return true是怎么回事呢?那么为什么不写
    return array.equals(terms,other.terms)
    没有让读者感到困惑吗?谢谢你的努力。我真的很感激,但它对我不起作用。幸运的是,我找到了自己的解决方案。我在Term类中编写了一个equals方法,然后在Poly类中的equals方法中使用它。在Poly equals方法中,我首先检查两个数组的长度是否相等。然后使用两个术语变量循环两个数组,以保存和比较每个数组的值。我在方法的最开始初始化了一个布尔值,相应地调整它,然后返回它。我稍后将发布代码。equals方法中是否有输入错误?我得到返回false在{}中对于最后一个if语句,但是底部的return true是怎么回事呢?那么为什么不写
    return array.equals(terms,other.terms)
    没有让读者感到困惑吗?谢谢你的努力。我真的很感激,但它对我不起作用。幸运的是,我找到了自己的解决方案。我在Term类中编写了一个equals方法,然后在Poly类中的equals方法中使用它。在Poly equals方法中,我首先检查两个数组的长度是否相等。然后使用两个术语变量在两个数组中循环,以保存和比较每个数组的值。我在方法的开头初始化了一个布尔值,相应地调整它,然后返回它。稍后我将发布代码。