Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing ArrayList get(i) to give parameters to create object doesn't work
    primarykey
    data
    text
    <p>I have this problem. I'm creating a polynomial program, where I can subtract, add and multiply. </p> <p>However I come across this error, I can't really pinpoint, why this happens. </p> <p>First I have my Polynomial.java , I create an arraylist of two inputs, then I sort them based on the exponent, then I loop through this list, if the exponent matches, than a new pair will be created based on adding the coefficients and using the original exponent. </p> <pre><code>public Polynoom subtract(Polynoom that) { Polynoom subtract = new Polynoom(); subtract.termen.addAll(that.termen); for (int i = 0; i &lt; subtract.termen.size(); i++) { subtract.termen.get(i).coef = subtract.termen.get(i).coef * -1; } subtract.termen.addAll(this.termen); Collections.sort(subtract.termen); subtract.removeDoubles(); return subtract; } </code></pre> <p>This will create a list of pairs, with a double and an integer. For example: </p> <pre><code>-1.0 2 -1.0 1 1.0 1 2.0 0 </code></pre> <p>Then I have a method that will look for pair with the same exponent, if so, then it will add the coefficient and takes the exponent of the first pair. </p> <pre><code> private void removeDoubles() { for (int i = 0; i &lt; this.termen.size() - 1; i++) { if (this.termen.get(i).exponent == this.termen.get(i + 1).exponent) { this.termen.set(i, new Pair((this.termen.get(i).coef + this.termen.get(i + 1).coef), (this.termen.get(i).exponent))); this.termen.remove(i + 1); i --; } } } </code></pre> <p>However, somehow the second argument I pass in the new Pair doesn't work. </p> <p>If i for example change the new Pair to: </p> <pre><code>this.termen.set(i, new Pair(13, 2)); result difference: 1.0x^2 + 13.0x^2 + 2.0 (this is correct) </code></pre> <p>If i for example change new Pair to: </p> <pre><code>this.termen.set(i, new Pair(13, this.termen.get(i).exponent); result difference: -1.0x^2 + x^1 + 2.0 (this is not correct) </code></pre> <p>Why if i set the new pair manually, it will work? Why doesn't it work why I use the this.termen.get.exponent? Because I basically do the same: putting an integer when creating a new Pair object. </p> <p>Can anyone give me a tip? :)</p> <p>Thanks!</p> <p>EDIT: </p> <p>My termen class, is part of my Polynomial class, and termen is an custom arraylist of the type pair. </p> <pre><code>class Polynoom implements PolynoomInterface { ArrayList&lt;Paar&gt; termen = new ArrayList&lt;Paar&gt;(); </code></pre> <p>And my pair class: </p> <pre><code>class Paar implements Comparable &lt;Paar&gt; { double coef; int exponent; Paar(double a, int b) { coef = a; exponent = b; } </code></pre> <p>EDIT 2: My toString method of class Polynomial: </p> <pre><code>public String toString() { String temp = ""; for (int i = 0; i &lt; this.termen.size(); i++) { if (i != this.termen.size() - 1) temp += this.termen.get(i) + " + "; else temp += this.termen.get(i); } return temp; } </code></pre> <p>It looks like it keep printing the old list, not the new one. I don't see why exactly. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload