简体   繁体   中英

Adding Objects to an ArrayList in java through multiple classes

I have looked through multiple other posts on this topic, so I apologize if I am reposting previously discussed things here. Here is one of the test methods my main method calls in my testing class:

public static void test1(){
        PolynomialA p = new PolynomialA();
        //should return true
        System.out.println(p.isEmpty());
        if(p.isEmpty()){
            p.addTerm(2, 3);
            p.addTerm(3, 2);
            //should print (2)x^(3) + (3)x^(2)
            System.out.println(p.toString());
            //should return false
            System.out.println(p.isEmpty());
        }
}

Now, for whatever reason, add term is not adding the terms, and the Arraylist stays empty. So, ill show you my constructor of PolynomialA:

public class PolynomialA implements Quantity{

   private ArrayList<Term> list; 

   PolynomialA(){
       list = new ArrayList<Term>();
   }//end constructor

And the addTerm() method where I am trying to add a Term(will show below) to the ArrayList list.

public void addTerm(int c, int e){
    Term temp;

    for(int i = 0; i < list.size(); i++){
        //if a term with this exponent already exists in the polynomial
        if(list.get(i).exponent == e){
            //just add the passed coefficient to the existing one
            temp = new Term((list.get(i).coefficient + c), e);
            list.set(i, temp);
        }//end if
        //else: a term with this exponent does not exist
        else{
            //add the new term with the passed coeff and expo to the end
            temp = new Term(c, e);
            list.add(temp);
        }//end else
    }//end for
}//end addTerm()

The entire Term Class:

public class Term{

    int coefficient;
    int exponent;

    Term(){
        coefficient = 0;
        exponent = 0;
    }

    Term(int c, int e){
        coefficient = c;
        exponent = e;
    }
}

So, basically, polynomial is an ArrayList of Term's, which a term has 2 integer values associated with it: coefficient, and exponent. There are many other methods throughout the PolynomialA class, but this is the first one I need working in order for any of the others to create one. For whatever reason, i cannot append a term to the empty ArrayList. I am getting no exceptions thrown or anything, just doesnt add the term to the ArrayList. PLEASE HELP

Also, I apologize if these snippits of code were put up here in a bass-ackwards way.

All of your logic is in a loop that iterates over the list. Initially the list is empty, so the loop body is never executed.

Your addTerm method should look something like this:

public void addTerm(int c, int e){
    for (Term term : list) { // loop over the existing terms
        if (term.exponent == e) { // if you find a matching one
            term.coefficient += c; // update it's coefficient

            return; // and return from the method
        }
    }

    list.add(new Term(c, e)); // add a new term if no matching one was found
}

In your addTerm class, you have a for loop, that goes from int i = 0 to i < list.size(). Your list is 0 at the beginning, so 0 < 0. You never go inside the loop. I would recommend splitting the logic of the loop. First check for the items, if you don't find the value already in there, Then you can add the value (outside the loop).

public void addTerm(int c, int e){
    Term temp;

    for(int i = 0; i < list.size(); i++){
        //if a term with this exponent already exists in the polynomial
        if(list.get(i).exponent == e){
            //just add the passed coefficient to the existing one
            temp = new Term((list.get(i).coefficient + c), e);
            list.set(i, temp);
        }//end if
        //else: a term with this exponent does not exist
        else{
            //add the new term with the passed coeff and expo to the end
            temp = new Term(c, e);
            list.add(temp);
        }//end else
    }//end for
}//end addTerm()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM