2

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;
        }
    }

}
1
  • 1
    Please elaborate on "the program don't work no more". You may wish to read out SO Question Checklist to help you ask a question that will get you a useful answer. Commented Sep 30, 2016 at 21:49

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm still not getting the correct answer. Why do you think term.size() might not returns the correct value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.