Reputation: 79
Edited I wrote a method that adds term objects to a polynomial class using array store the term objects, now I want to use ArrayList instead of array. I changed the array to arraylist using get() and add(), but the program is not giving me the correct answer no more. Here's the method after I changed:
public void addTerm(int c, int e) {
for (int i = 0; i < terms.size(); i++) {
// if term with exponent not exist
if (terms.get(i) == null) {
// no coefficients of zero are stored
if (c == 0) {
return;
}
terms.add(new Term(c, e) );
return;
}
// if term with exponent already exist
if (terms.get(i).getExponent() == e) {
int coe = terms.get(i).getCoefficient();
int newCoe = coe + c;
// no coefficients of zero are store
if (newCoe == 0) {
terms.add(null);
return;
}
terms.get(i).setCoefficient(newCoe);
return;
}
}
}
Here's the original code:
public void addTerm(int c, int e) {
for (int i = 0; i < termSize; i++) {
// if term with exponent not exist
if (terms[i] == null) {
// no coefficients of zero are store
if (c == 0) {
return;
}
terms[i] = new Term(c, e);
return;
}
// if term with exponent already exist
if (terms[i].getExponent() == e) {
int coe = terms[i].getCoefficient();
int newCoe = coe + c;
// no coefficients of zero are store
if (newCoe == 0) {
terms[i] = null;
return;
}
terms[i].setCoefficient(newCoe);
return;
}
}
}
Upvotes: 2
Views: 366
Reputation: 527
terms.add(i,new Term(c, e) );
this adds an object at the specified position (if you have [4,5,6] and add element 9 at position 2 you get [4,5,9,6]) i think what you want to do is set the value, at this position
terms.set(i,new Term(c, e) );
edit: also have you made sure that terms.size() returns the correct value?
You shoudnt just translate Array code to List code. They are quite different. Arrays should be used if you have a fixed number of objects and you know the size beforehand. Lists should be used if the number of objects changes / isn't fit. Arrays have set positions for every object, whereas lists normally just add at the end.
edit after you edited the question:
First of all, if you edit your question please add the changes at the end of you original post. Dont change your original question (it is confusing for others reading you post later on).
Secondly, if the code above is your new code it is likely term.size() returns a different value than termSize. The reason is in your array version, the size of the array is never changed (its fixed). Whereas in the List version terms.add(new Term(c, e) ); increases the List size by one.
Also we dont know how your array/list is initiallised. If you do something like
Term[] terms = new Term[100];
your array size is 100, but if you do a normal initialization for a list
ArrayList<Term> terms = new ArrayList<>();
its size is 0 and you would have to add 100 null objects to get the same result.
for(int i=0; i<100; i++){
terms.add(null);
}
(BUT PLS DON'T DO THIS, IT IS BAD CODING)
So please explain why you want to change Array Code to List Code? Arrays and Lists are used in different situations. They are used for different problems. What is the resason for directly translating Array Code to List Code?
Maybe if you explain what you want to do, we can give you a better solution for your problem.
Upvotes: 2