2016-10-10 6 views
1

このトピックでは他の複数の記事を調べていますので、ここで先に説明したことを再投稿してもお詫び申し上げます。ここに私の主な方法は、私のテストクラスで呼び出すテストメソッドのいずれかです。何らかの理由で今複数のクラスを通してjavaのArrayListにオブジェクトを追加する

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 

そして、私は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; 
    } 
} 

は、基本的に、多項式の用語は、それに関連付けられた2つの整数値を有し、この用語ののArrayListのです。 PolynomialAクラスには他にもたくさんのメソッドがありますが、これは他のメソッドが作成するために必要な最初のメソッドです。何らかの理由で、私は空のArrayListに用語を追加できません。私は例外がスローされたり、何も得られていない、単にArrayListに用語を追加しません。助けてください

また、私はこれらのスニペットコードがここにベースアックの方法で置かれた場合は謝罪します。

+0

アレイリストを初期化するとき、私は "無言"のarraylistを作成する必要があります。そうでなければ、coeffとexpoで1つだけ作成したでしょう。 – anon

+0

あなたはループについて共通の初心者間違いをしました。その指数を持つ項がまだない場合は、その項を追加します。その指数を持つ項があるかどうかを知ることはできません。あなたがリスト全体を見ていればいいでしょうか?したがって、その用語をリストに追加するコードは、forループの後に_ _ _ _ _ _ _ _ _ _ _ _起こる必要があります。 – ajb

+1

@Joe答えが得られたら質問を削除するのは、それが動作する方法ではありません。ロールバック。 –

答えて

2

すべてのロジックがループを繰り返してリストを反復します。最初はリストは空であるため、ループ本体は決して実行されません。

addTerm方法は、次のようなものになりますあなたの:あなたの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

を、あなたはループのために持っている、それは、I = 0のint iから<はlist.size()に行きます。あなたのリストは最初は0なので、0 <です。あなたは決してループの中にはいません。私はループのロジックを分割することをお勧めします。最初に値が見つからない場合はアイテムをチェックしてから、値を追加することができます(ループ外)。

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

あなたのコードはOPとどのように違うのですか? –

関連する問題