I want to merge two arraylists to one new arraylist, and iterate through the list. Before I make actions on my new arraylist, I want to multiple some value times -1. The problem however is, that when using addAll() I also multiple my old arraylist.
public Polynoom subtract (Polynoom that) {
Polynoom subtract = new Polynoom();
subtract.termen.addAll(that.termen);
for (int i = 0; i < 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;
}
My Polynoom class looks like this:
class Polynoom implements PolynoomInterface {
ArrayList<Paar> termen = new ArrayList<Paar>();
Polynoom() {
termen = new ArrayList<Paar>();
}
}
The problem is (i think): that when using addAll() it creates REFERENCES instead of new copies, therefore changing the elements will change both. What is nice way to overcome this problem?
I can of course create a new pair instead of a Polynoom an add this pair to a new Polynoom but I don't think that is really nice code. Does someone has a better idea?