Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.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自定义类中的toString方法_Java_Arraylist - Fatal编程技术网

Java 使用ArrayList自定义类中的toString方法

Java 使用ArrayList自定义类中的toString方法,java,arraylist,Java,Arraylist,我在我的程序中使用了三个类: 术语带有变量系数和指数的类,toString()方法等 Polynome类,使用ArrayList存储不同的术语对象 运行程序的主类 我可以在我的Polynome类中使用ArrayList的toString方法吗?我正在努力,但我做不到 我需要我的多项式像这样输出:[3x^2,3x^1,1x^0] 我真的很困惑,我调用了Term的toString方法,使用for循环分别访问每个Term 我的代码: public class Term { private

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

  • 术语
    带有变量系数和指数的类,toString()方法等
  • Polynome
    类,使用ArrayList存储不同的
    术语
    对象
  • 运行程序的主类
我可以在我的Polynome类中使用ArrayList的
toString
方法吗?我正在努力,但我做不到

我需要我的多项式像这样输出:[3x^2,3x^1,1x^0]

我真的很困惑,我调用了Term的toString方法,使用for循环分别访问每个Term

我的代码:

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;
    }
}
多项式类:

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?
    }
}
公共类{
私人ArrayList polynoom;
公共图书馆(){
polynoom=新的ArrayList();
}
公共无效添加(期限){
添加(术语);
}
公共术语get(int i){
返回polynoom.get(i);
}
公共整数大小(){
返回polynoom.size();
}
公共字符串toString(){
//我应该在这里写什么?
}
}
主要类别:

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());
    }
}
公共类opgave3{
公共静态void main(字符串[]args){
波利尼奥姆波利尼奥姆1、波利尼奥姆2、索姆波利尼奥姆;
polynoom1=新的Polynoom();
新增(新术语(1,2));
新增(新术语(3,1));
添加(新的术语(1,0));
polynoom2=新的Polynoom();
添加(新术语(-1,3));
新增(新术语(2,2));
添加(新术语(-5,0));
System.out.println(“测试:”);
System.out.println(polynoom1.toString());
对于(int i=0;i
您只需使用
ArrayList
toString()
方法作为
Polynome
toString()方法的结果

public class Polynome {
    public ArrayList<Term> terms;

    @Override
    public String toString() {
        if (terms != null) {
            return terms.toString(); 
        } else {
            return "";
        }
    }
}
公共类多项式{
公共列表术语;
@凌驾
公共字符串toString(){
如果(术语!=null){
返回条件。toString();
}否则{
返回“”;
}
}
}

长话短说,您始终可以在任何东西上使用
toString()
,即使它是用户定义的类。当您调用该方法时,它将调用最近父类的
toString()
方法,该方法保证存在,因为
Object
有一个方法。如果要控制对象上调用的
toString()
的输出,必须覆盖它。实际上,如果对象的成员类型为
ArrayList
,那么调用对象的
toString()
将包含大量您可能不想要的额外信息。为了得到您想要的输出,您需要@Tenner的答案给出的代码,即

public class Polynome {
    public ArrayList<Term> terms;

    @Override
    public String toString() {
        if (terms != null) {
            return terms.toString(); 
        } else {
            return "";
        }
    }
}
公共类多项式{
公共列表术语;
@凌驾
公共字符串toString(){
如果(术语!=null){
返回条件。toString();
}否则{
返回“”;
}
}
}
但是您还需要重写
术语
类中的
toString()
,以便每个术语以所需的形式输出。之所以需要这样做,是因为当您在
ArrayList
或任何其他容器上调用
toString()
时,它会遍历容器,依次调用每个对象的
toString()
,添加容器类定义的任何格式。最终,将调用
Term
toString()
,您可以通过在
Term
类中重写它来控制输出


至于问题的最后一部分,您不需要直接调用
Term
toString()
,因为调用
ArrayList
toString()
方法将自己完成这项工作。

编辑:快速回答是,因为您将代码放到

return polynoom.toString();
你已经指出的地方。然后在你的主课上你可以简单地写

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;
    }
}

@Override toString()
添加到
术语
多项式
类中。
Term
class
toString()
应以系数x^exponent的格式返回字符串

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

publicstaticvoidmain(字符串[]args)引发异常{
Polynome Polynome=新的Polynome();
多项式项(3,2);
多项式项(3,1);
多项式项(1,0);
系统输出println(多项式);
}
公共静态类术语{
私人智力系数;
私有整数指数;
公共术语(int c,int e){
系数=c;
指数=e;
}
@凌驾
公共字符串toString(){
回报系数+“x^”+指数;
}
}
公共静态类多项式{
私有列表项=新的ArrayList();
公共无效添加项(int系数,int指数){
添加(新项(系数、指数));
}
@凌驾
公共字符串toString(){
返回条件。toString();
}
}
结果:


您忘记发布代码。“我可以在我的Polynome类中使用ArrayList的toString方法吗?”是的,要获得解释代码问题的更详细答案,我们需要先查看。要向问题添加更多信息,请使用选项(只是说因为缺少
编辑器
徽章表明您从未使用过它,所以可能您不知道)。谢谢大家,这很有意义。
返回terms.toString();
应该与
返回数组.toString(terms.toArray());
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();
    }
}