创建和添加项目以列出JAVA

时间:2015-04-22 21:06:15

标签: java arraylist insert add

我正在尝试创建字符串并将其添加到列表中

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 类中的列表。

字符串将包含已经是字符串的多项式。

1 个答案:

答案 0 :(得分: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(...);
}