English 中文(简体)
在ArrayList增加重复
原标题:Adding duplicates in an ArrayList

我不敢在阿雷拉派找到重复的条目,然后加以操纵。 我正在执行一个多语种,我想增加一门单剧,以创造新的多语言。 omi有学位和系数。 我想通过收集单米,找到拥有同样权力和增加系数的单米。 所有这些诸如有权力的垄断企业的总和将是多功能的。

问题回答

天体物理学家(或任何清单)接受重复。

然而,由于你想要分组 凭借其权力,您可考虑使用<代码>Map<Integer,Foo>,其中钥匙为权力。 F有许多选择。 Foo可以是ArrayList<Monomial>, a ArrayList<Double>,只持有 co,后加。 这要求贵方采用某种文字,或者使用第三部分图书馆进行多功能制。

或者,“Foo”可能是一种双倍,它代表了所总结的系数,在这种情况下,你需要书写一种补充(Monomial)方法,每当日更新一次双倍。

如果可能的权力范围很小而且众所周知,你也可以使用一个简单阵列。

在此我的解决办法中,你可以增加两个同等程度的多米。 b。

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() + "
" + p1.ToString() + "

" + (p.add(p1)).ToString());
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签