使用ArrayLists在自定义类中的toString方法

时间:2015-05-10 21:22:44

标签: java arraylist

我在我的程序中使用了三个类:

  • Term具有变量系数和指数的类,toString()方法等。
  • Polynome类,使用ArrayList存储不同的Term对象。
  • Main运行该程序的类。

我可以在Polynome类中使用ArrayList的toString方法吗?我正在努力,但我不能。

我需要我的多项式输出如下:[3x ^ 2,3x ^ 1,1x ^ 0]

我真的很困惑,我正在调用Term的toString方法,使用for循环分别访问每个术语。

我的代码:

public class Term {
    private int coëfficiënt;
    private int exponent;

    public Term(int coëfficiënt, int exponent) {
        this.coëfficiënt = coëfficiënt;
        this.exponent = exponent;
    }

    public int getCoef() {
        return coëfficiënt;
    }

    public int getExp() {
        return exponent;
    }

    public String toString() {
        return coëfficiënt + "x^" + exponent;
    }
}

Polynome类:

public class Polynoom {
    private ArrayList<Term> polynoom;

    public Polynoom() {
        polynoom = new ArrayList<Term>();
    }

    public void add(Term term) {
        polynoom.add(term);
    }

    public Term get(int i) {
        return polynoom.get(i);
    }

    public int size() {
        return polynoom.size();
    }

    public String toString() {
        // what should I write here?
    }
}

主要课程:

public class opgave3 {
    public static void main(String[] args) {
        Polynoom polynoom1, polynoom2, sompolynoom;

        polynoom1 = new Polynoom();
        polynoom1.add(new Term(1, 2));
        polynoom1.add(new Term(3, 1));
        polynoom1.add(new Term(1, 0));

        polynoom2 = new Polynoom();
        polynoom2.add(new Term(-1, 3));
        polynoom2.add(new Term(2, 2));
        polynoom2.add(new Term(-5, 0));

        System.out.println("Tests: ");
        System.out.println(polynoom1.toString());
        for (int i = 0; i < polynoom1.size(); i++) {
            System.out.println(polynoom1.get(i).toString());
        }
        System.out.println(polynoom1.get(0).toString());
    }
}

3 个答案:

答案 0 :(得分:4)

您只需使用 struct records { char first[20]; char last[20]; float rate; int zip; struct node* next; }; void main() { int i,n; printf("Please indicate the number of records : "); scanf("%d",&n); struct records *head,*conductor; head=(struct records*)malloc(n*sizeof(struct records)); head->next=NULL; for (i=0;i<n;i++){ printf("\nEnter employee information in the format :\nFirstname Lastname rate Zipcode (newline for the next)\n"); scanf("%s %s %f %d",&head->first,&head->last,&head->rate,&head->zip); conductor=head; conductor=conductor->next;} } 的{​​{1}}方法作为ArrayList的{​​{1}}方法的结果。

toString()

答案 1 :(得分:2)

编辑:快速回答,因为你把你的代码放在了

return polynoom.toString();

你指明的地方。然后在您的Main类中,您只需编写

System.out.println(polynoom1);

以所需格式显示内容。

正如Tenner所说,使用ArrayList的toString()方法获得所需的输出。但是也要确保你的Term类有一个有用的toString方法:

public class Term {
    private int co, ex;

    public Term(int coeff, int exp) {
        co = coeff;
        ex = exp;
    }

    @Override
    public String toString() {
        return co + "x^" + ex;
    }
}

答案 2 :(得分:2)

@Override toString()添加到Term&amp; Polynome课程。 TermtoString()应返回系数 x ^ exponent 格式的字符串。

然后让PolynometoString()返回yourArrayList.toString()

public static void main(String[] args) throws Exception {
    Polynome polynome = new Polynome();
    polynome.addTerm(3, 2);
    polynome.addTerm(3, 1);
    polynome.addTerm(1, 0);
    System.out.println(polynome);
}

public static class Term {
    private int coefficient;
    private int exponent;

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

    @Override
    public String toString() {
        return coefficient + "x^" + exponent;
    }
}

public static class Polynome {
    private List<Term> terms = new ArrayList<>();

    public void addTerm(int coefficient, int exponent) {
        terms.add(new Term(coefficient, exponent));
    }

    @Override
    public String toString() {
        return terms.toString();
    }
}

结果:

enter image description here