2015-04-22 88 views
-1

我試着去創建並添加字符串列表創建和添加項目列出JAVA

package the.arraylist.pkgclass; 

import java.util.ArrayList; 

/** 
A class to implement a Polynomial as a list of terms, where each term has 
an integer coefficient and a nonnegative integer exponent 

@author your name 
*/ 
public 
     class Polynomial 
{ 
// instance variable declarations go here 
Term theTerm ; //initializes a term 

/** 
Creates a new Polynomial object with no terms 
*/ 
public 
     Polynomial() 
{ 
    // TO DO: Write constructor body here 

    ArrayList<Term> list1 = new ArrayList<>(); 
} 

/** 
Inserts a new term into its proper place in a Polynomial 

@param coeff the coefficient of the new term 
@param expo the exponent of the new term 
*/ 
public 
     void insert (int coeff , int expo) 
{ 
    // TO DO: write method body here. 
} 

我不覺得我已經正確初始化列表,因爲我不能在名單上調用insert class。

這些字符串將包含已經是字符串的多項式。

+0

不確定你的意思是「字符串將包含已經是字符串的多項式」 – Claudio

回答

1

您應該保留您的列表作爲您的Polynomial類的屬性,以便您稍後可以向其中添加項目。

public class Polynomial { 

private List<Term> list; 

public Polynomial() { 
    this.list = new ArrayList<>(); 
} 

public void insert (int coeff , int expo) { 
    this.list.add(...); 
} 
+0

這是問題所在。非常感謝你。現在一切似乎都奏效。 – dchar028