通过多个类将对象添加到java中的ArrayList

时间:2016-10-10 03:56:16

标签: java oop arraylist add

我已经查看了有关此主题的其他多篇帖子,所以如果我在此重新发布之前讨论的内容,我会道歉。这是我的main方法在我的测试类中调用的测试方法之一:

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

现在,无论出于何种原因,添加术语不会添加术语,并且Arraylist保持为空。所以,生病告诉你我的PolynomialA的构造函数:

public class PolynomialA implements Quantity{

   private ArrayList<Term> list; 

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

我正在尝试将一个Term(将在下面显示)添加到ArrayList列表的addTerm()方法。

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()

整个术语类:

public class Term{

    int coefficient;
    int exponent;

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

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

因此,基本上,多项式是Term的ArrayList,其中一个术语有2个与之关联的整数值:系数和指数。 PolynomialA类中还有许多其他方法,但这是我需要工作的第一个方法,以便其他任何方法创建一个。无论出于何种原因,我都无法在空ArrayList中附加一个术语。我没有抛出任何异常或任何东西,只是没有将该术语添加到ArrayList。请帮助

另外,如果这些代码的代码以低音方式放在这里,我道歉。

2 个答案:

答案 0 :(得分:2)

您的所有逻辑都在一个遍历列表的循环中。最初列表为空,因此循环体永远不会执行。

您的addTerm方法应如下所示:

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
}

答案 1 :(得分:1)

在你的addTerm类中,你有一个for循环,从int i = 0到i&lt;则为list.size()。你的清单在开头是0,所以0&lt;你永远不会进入循环。我建议拆分循环的逻辑。首先检查项目,如果你没有找到那里的值,那么你可以添加值(在循环外)。

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()