在ArrayList中添加重复项

时间:2011-11-11 00:50:56

标签: java collections

我被困在如何在ArrayList中找到重复的条目然后操纵它们。我正在实现一个多项式类,我想添加相同顺序的单项式来创建一个新的多项式。单项式具有度和系数。我想循环遍历一个单项式的集合,找到具有相同幂的单项式并添加系数。所有这些像动力单项式的总和将是多项式。

2 个答案:

答案 0 :(得分:1)

ArrayList(或任何List)接受重复。

但是,由于您希望按功率对Monomials进行分组,因此您可以考虑使用密钥为幂的Map<Integer,Foo>。 Foo有很多选择。 Foo可能是ArrayList<Monomial>ArrayList<Double>,仅包含系数,您稍后会添加。这需要您编写一些代码,或者使用MultiMap的第三个部分库。

或者,Foo可以是一个表示求和系数的Double,在这种情况下,您需要编写一个add(Monomial)方法,每次都更新Double。

如果可能的功率范围很小且已知,您也可以使用简单的数组。

答案 1 :(得分:-1)

这是我的解决方案,你可以添加两个相同程度的多项式。注意错误。

public class Polynome {


    private double[] coefficients;
    private int degre;



    public Polynome(int degre) {

        if (degre < 0) {
            System.out.println("Invalid Degree");
        }

        this.degre = degre;
        coefficients = new double[degre + 1];
        for (int i = 0; i < degre; i++)
            coefficients[i] = 0;

        coefficients[degre] = 1;
    }


    public void setCoefficient(int degre, double coefficient) {
        if (degre < 0 || degre > this.degre) {
            System.out.println("Invalid Degree");
        }

        if (coefficient == 0 && degre == this.degre && this.degre != 0) {
            System.out.println("Null Degree");
        }

        coefficients[degre] = coefficient;
    }

    /*
     * Returns the coeff giving the degree of the element
     */
    public double getCoefficient(int degre) {
        if (degre < 0 || degre > this.degre) {
            return 0;
        }

        if (degre == this.degre && this.degre != 0) {
            return coefficients[this.getDegre()];
        }

        return this.coefficients[degre];
    }



    public String ToString() {

        if (degre == 0)
            return "" + this.coefficients[0];

        String result = "" + this.coefficients[degre]+" x^" + degre;
        for (int i = degre-1  ; i > 0 ; i--){
            if (this.coefficients[i] < 0) {
                result += "-" + Math.abs(this.coefficients[i]) + " x^" + i;
            }
            else {
                if (this.coefficients[i] > 0){
                    result += " + " + this.coefficients[i] + " x^" + i;
                }
            }
        }

        if (this.coefficients[0]!= 0) result += " + " + this.coefficients[0]  ;
        return result;
    }



    /*
     * Returns the degree of the current poly
     */
    public int getDegre() {
        return degre;
    }


    /*
     * Adds two Polys with the same degrees
     */
    public Polynome add(Polynome p) {
        Polynome result = new Polynome(p.getDegre());
        int deg = result.getDegre();


        for(int i = deg  ; i >0 ; i--) {
            result.coefficients[i] = this.getCoefficient(i) + p.getCoefficient(i);
        }

        return result;
    }






    public static void main(String...args) {
        Polynome p = new Polynome(2);
        p.setCoefficient(2, 7);

        Polynome p1 = new Polynome(2);
        p1.setCoefficient(2, 2);

        System.out.println(p.ToString() + "\n" + p1.ToString() + "\n\n" + (p.add(p1)).ToString());
    }
}