Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过多个类向java中的ArrayList添加对象_Java_Oop_Arraylist_Add - Fatal编程技术网

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

通过多个类向java中的ArrayList添加对象,java,oop,arraylist,add,Java,Oop,Arraylist,Add,我已经浏览了很多关于这个话题的帖子,所以如果我在这里转载之前讨论过的事情,我很抱歉。以下是我在测试类中主要方法调用的测试方法之一: public static void test1(){ PolynomialA p = new PolynomialA(); //should return true System.out.println(p.isEmpty()); if(p.isEmpty()){ p.addT

我已经浏览了很多关于这个话题的帖子,所以如果我在这里转载之前讨论过的事情,我很抱歉。以下是我在测试类中主要方法调用的测试方法之一:

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());
        }
}
现在,不管出于什么原因,addterm并没有添加这些术语,而Arraylist保持为空。因此,我将向您展示我的多项式构造函数:

public class PolynomialA implements Quantity{

   private ArrayList<Term> list; 

   PolynomialA(){
       list = new ArrayList<Term>();
   }//end constructor
所以,基本上,多项式是项的数组列表,一个项有两个整数值:系数和指数。在整个PolynomialA类中还有许多其他方法,但这是我需要使用的第一个方法,以便其他任何方法创建一个。无论出于何种原因,我都无法在空的ArrayList中追加一个术语。我没有得到抛出的异常或任何东西,只是没有将术语添加到ArrayList中。请帮忙


此外,如果这些代码片段是以bass ackwards的方式放在这里的,我很抱歉

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

您的
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
}

在addTerm类中,有一个for循环,从int i=0到i
public void addTerm(int c,int e){
术语温度;
对于(int i=0;i
初始化数组列表时,我必须创建一个“无条件”数组列表。否则我会创建一个coeff和expo为0的循环。你在循环方面犯了一个常见的新手错误。如果还没有具有该指数的项,则需要添加该项。在查看整个列表之前,您无法判断是否存在具有该指数的术语,对吗?因此,将术语添加到列表中的代码必须发生在for循环之后,而不是在for循环内部。@Joe在得到答案时删除您的问题不是它的工作方式。后退。
public class Term{

    int coefficient;
    int exponent;

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

    Term(int c, int e){
        coefficient = c;
        exponent = e;
    }
}
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
}
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()