在数组元素中随机获取null

时间:2017-05-04 15:58:41

标签: java arrays

当我使用Polynomial构造函数创建Monomial数组时,有时也是如此 我得到null对象而不是Monomial对象,有时我得到实际的Monomial。

我不明白为什么

我调试代码,在Polynomial构造函数中我构造了特定的对象,但是当我返回main方法时,有时我得到Monomial的特定数组,有时我在数组中得到一个null。

TestPolynomials.java

public class TestPolynomials
{
   public static void main(String[] args)
   {
    // 13b^2x^3z
    Monomial m1 = new Monomial(13);
    m1.setDegree('b', 2);
    m1.setDegree('x', 3);
    m1.setDegree('z', 1);
    // 15
    Monomial m2 = new Monomial(15);
    Polynomial p1 = new Polynomial(new Monomial[] { m1, m2 });
   }
}

Monomial.java

public class Monomial
{
    private int coefficient;
    private int[] var;
    public Monomial(int coefficient)
    {
        this.coefficient = coefficient;
        this.var = new int[26];
    }
    public int getCoefficient()
    {
        return coefficient;
    }
    public void setCoefficient(int coefficient)
    {
        this.coefficient = coefficient;
    }
    public int getDegree(char variable)
    {
        return var[variable - 'a'];
    }
    public void setDegree(char variable, int degree)
    {
        this.var[variable - 'a'] = degree;
    }
    public Monomial getCopy()
    {
        Monomial monom = new Monomial(this.coefficient);
        for (char ch = 'a'; ch <= 'z'; ch++)
        {
            if (this.getDegree(ch) > 0)
                monom.setDegree(ch, this.getDegree(ch));
        }
        return monom;
    }
    public String toString()
    {
        String monom = "";
        monom += Integer.toString(this.getCoefficient());
        for (char ch = 'a'; ch <= 'z'; ch++)
        {
            if (this.getDegree(ch) > 0)
            {
                monom += ch;
                if (this.getDegree(ch) > 1)
                {
                    monom += "^";
                    monom += Integer.toString(this.getDegree(ch));
                }
            }
        }
        return monom;
    }
}

Polynomial.java

public class Polynomial
{
    private Monomial[] monomials;
    public Polynomial(Monomial[] monomials)
    {
        this.monomials = new Monomial[monomials.length];
        for (int i = 0; i < monomials.length; i++)
        {
            this.monomials[i] = monomials[i].getCopy();
        }
    }

    public String toString()
    {
        String str = "";
        for (int i = 0; i < this.getMonomialCount(); i++)
        {
            str += this.getMonomial(i).toString();
            str += "+";
        }
        return str;
    }
}

我构建了m1和m2,我尝试构建p1。

有时我会得到这个:[13b^2x^3z, 15],有时我会得到这个:[null, 30]

为什么?

感谢。

=============================================== ========================= 编辑:这是我的完整代码,当我运行它时,我在控制台中得到它

m1 = 13b^2x^3z
m2 = 15
m3 = -4b^2x^3z
m4 = 10b^3x^4z^2
13b^2x^3z does not have the same degrees as 15
13b^2x^3z has the same degrees as -4b^2x^3z
Exception in thread "main" java.lang.NullPointerException
    at Polynomial.toString(Polynomial.java:98)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at TestPolynomials.main(TestPolynomials.java:44)

TestPolynomials.java

public class TestPolynomials
{
private static String tempstr = ""; // For printing results;
public static void main(String[] args)
    {
        // 13b^2x^3z
        Monomial m1 = new Monomial(13);
        m1.setDegree('b', 2);
        m1.setDegree('x', 3);
        m1.setDegree('z', 1);
        System.out.println("m1 = " + m1);
        // 15
        Monomial m2 = new Monomial(15);
        System.out.println("m2 = " + m2);
        // -4b^2x^3z
        Monomial m3 = new Monomial(-4);
        m3.setDegree('b', 2);
        m3.setDegree('x', 3);
        m3.setDegree('z', 1);
        System.out.println("m3 = " + m3);
        Monomial m4 = new Monomial(10);
        m4.setDegree('b', 3);
        m4.setDegree('x', 4);
        m4.setDegree('z', 2);
        System.out.println("m4 = " + m4);
        if (m1.hasSameDegrees(m2))
            tempstr = " has";
        else
            tempstr = " does not have";
        System.out.println(m1 + tempstr + " the same degrees as " + m2);
        if (m1.hasSameDegrees(m3))
            tempstr = " has";
        else
            tempstr = " does not have";
        System.out.println(m1 + tempstr + " the same degrees as " + m3);
        Polynomial p1 = new Polynomial(new Monomial[] { m1, m2 });
        Polynomial p2 = new Polynomial(new Monomial[] { m3 });
        Polynomial p3 = p1.add(p2);
        System.out.println("p1 = " + p1); // should be 13b^2x^3z+15
        System.out.println("p2 = " + p2); // should be -4b^2x^3z
        System.out.println("p3 = " + p3); // should be 13b^2x^3z+15-4b^2x^3z up to reordering the monomials
        System.out.println("-p3 = " + p3.inverse()); // should be -13b^2x^3z-15+4b^2x^3z up to reordering the monomials
        System.out.println("p4 =  p3 + p2");
        Polynomial p4 = p3.add(p2);
        int[] assignment = { 0, 1, 6, 4, 3, 0, 0, 2, 3, 5, 2, 6, 3, 8, 7, 0, 0, 4, 2, 6, 0, 4, 1, 5, 1, 9 };
        System.out.println("p4  value = " + p4.computeValue(assignment) + " --- before normalization");
        p4.normalize();
        System.out.println("p4 = " + p4);
        System.out.println("p4 value = " + p4.computeValue(assignment) + " --- after normalization");
        Polynomial p5 = new Polynomial(new Monomial[] { m1, m2, m3, m4, m1, m2, m3, m4, m4 });
        System.out.println("p5 items are m1,m2,m3,m4,m1,m2,m3,m4,m4");
        System.out.println("p5 = " + p5);
        Monomial m5 = new Monomial(0);
        m5.setDegree('z', 1);
        Polynomial p6 = new Polynomial(new Monomial[] { m5 });
        System.out.println("p6 = " + p6);
        Polynomial p7 = new Polynomial(new Monomial[] {});
        System.out.println("p7 = " + p7);
    }

}

Monomial.java

public class Monomial
{
    private int coefficient;
    private int[] var;
    public Monomial(int coefficient)
    {
        this.coefficient = coefficient;
        this.var = new int[26];
    }
    public int getCoefficient()
    {
        return coefficient;
    }
    public void setCoefficient(int coefficient)
    {
        this.coefficient = coefficient;
    }
    public int getDegree(char variable)
    {
        return var[variable - 'a'];
    }
    public void setDegree(char variable, int degree)
    {
        this.var[variable - 'a'] = degree;
    }
    public boolean hasSameDegrees(Monomial other)
    {
        for (char ch = 'a'; ch <= 'z'; ch++)
        {
            if (!(this.getDegree(ch) == other.getDegree(ch)))
                return false;
        }
        return true;
    }
    public Monomial getCopy()
    {
        Monomial monom = new Monomial(this.coefficient);
        for (char ch = 'a'; ch <= 'z'; ch++)
        {
            if (this.getDegree(ch) > 0)
                monom.setDegree(ch, this.getDegree(ch));
        }
        return monom;
    }
    public String toString()
    {
        String monom = "";
        monom += Integer.toString(this.getCoefficient());
        for (char ch = 'a'; ch <= 'z'; ch++)
        {
            if (this.getDegree(ch) > 0)
            {
                monom += ch;
                if (this.getDegree(ch) > 1)
                {
                    monom += "^";
                    monom += Integer.toString(this.getDegree(ch));
                } 
            }
        }
        return monom;
    }
 }

Polynomial.java

import java.lang.Math;
public class Polynomial
{
    private Monomial[] monomials;
    public Polynomial(Monomial[] monomials)
    {
        this.monomials = new Monomial[monomials.length];
        for (int i = 0; i < monomials.length; i++)
        {
            this.monomials[i] = monomials[i].getCopy();
        }
    }
    public int getMonomialCount()
    {
        return this.monomials.length;
    }
    public Monomial getMonomial(int index)
    {
        if (index <= this.getMonomialCount())
            return this.monomials[index];
        return null;
    }
    public Polynomial inverse()
    {
        Monomial[] inverse = new Monomial[this.getMonomialCount()];
        for (int i = 0; i < inverse.length; i++)
        {
            inverse[i] = this.monomials[i].getCopy();
            inverse[i].setCoefficient(inverse[i].getCoefficient() * -1);
        }
        return new Polynomial(inverse);
    }
    public Polynomial add(Polynomial other)
    {
        Monomial[] add = new Monomial[this.getMonomialCount() + other.getMonomialCount()];
        for (int i = 0; i < other.getMonomialCount(); i++)
        {
            add[i] = other.getMonomial(i).getCopy();
        }
        for (int i = other.getMonomialCount(); i < add.length; i++)
        {
            add[i] = this.monomials[i - other.getMonomialCount()].getCopy();
        }
        return new Polynomial(add);
    }
    public int computeValue(int[] assignment)
    {
        int result = 1, sum = 0;
        for (int i = 0; i < this.getMonomialCount(); i++)
        {
            for (char ch = 'a'; ch <= 'z'; ch++)
            {
                if (this.getMonomial(i).getDegree(ch) > 0)
                    result *= (int) Math.pow((double) assignment[i], (double) this.getMonomial(i).getDegree(ch));
            }
            result *= this.getMonomial(i).getCoefficient();
            sum += result;
            result = 1;
        }
        return sum;
    }
    public void normalize()
    {
        Monomial[] normalize = new Monomial[this.getMonomialCount()];
        for (int i = 0; i < this.getMonomialCount(); i++)
        {
            for (int j = 1; j < this.getMonomialCount(); j++)
            {
                if (this.getMonomial(i).hasSameDegrees(this.getMonomial(j)))
                {
                    normalize[i] = new Monomial(this.getMonomial(i).getCoefficient() + this.getMonomial(j).getCoefficient());
                    for (char ch = 'a'; ch <= 'z'; ch++)
                    {
                        if (this.getMonomial(i).getDegree(ch) > 0)
                            normalize[i].setDegree(ch, this.getMonomial(i).getDegree(ch));
                    }
                    this.monomials[i] = null;
                }
            }
        }
        this.monomials = normalize;
    }
    public String toString()
    {
        normalize();
        String str = "";
        for (int i = 0; i < this.getMonomialCount(); i++)
        {
            str += this.getMonomial(i).toString();
            str += "+";
        }
        return str;
    }
}

为什么?

0 个答案:

没有答案