如何为ArrayList编写toString方法?

时间:2017-02-13 22:44:51

标签: java arraylist tostring

我很难找到如何编写我的toString方法来获取我的程序中每个熊的输出。我希望输出显示" Race - Points - TotalPoints"。但即使代码的其余部分似乎正在编译,也无法使其正确。

我是否需要在两个类中定义toString或者我缺少什么?我已经检查了几个类似的问题,这似乎是一个替代问题?但它如何最有效地实施?

首先关闭熊类:

import java.util.ArrayList;

public class Bear {

    public static void main(String[] args) {

        Bear b = new Bear("Sebastian", 100, "Brownbear");
        ArrayList <Bear> bears = new ArrayList<Bear>();
        bears.add(b);

    }

    private String name;
    private int points;
    private String race;


    public Bear(String name, int points, String race) {
        this.name = name;
        this.points = points;
        this.race = race;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRace() {
        return race;
    }

    public void setRace(String race) {
        this.race = race;
    }

    public int getInitialPoints() {
        return points;
    }

    public int getPoints() {
        int oldPoints = points;
        points /= 2;
        return oldPoints;
    }
}

其次是BearCollection类:

import java.util.ArrayList;

public class BearCollection {

    ArrayList <Bear> bears = new ArrayList<Bear>();

     int totalPoints = 0;

    public void add (Bear b) {

        for (Bear inCollection : bears) {
            if(b.getName().equals(inCollection.getName())) {
                return;
            }
        }

        for (Bear inCollection : bears)
        if (b.getRace().equals(inCollection.getRace())) {

            for(int i =  bears.size(); i > 0; i --) {
                if(bears.get(i).getRace().equals(b.getRace())) {
                    b.getPoints(); 
                    i = 0;
                } 
            }
        }

        totalPoints += b.getInitialPoints();


        bears.add(b) ;
    }

public String toString(){

    return ;
}

3 个答案:

答案 0 :(得分:0)

覆盖Bear类中的toString将解决问题。

答案 1 :(得分:0)

正如您所知,只需覆盖toString方法即可。性能使用StringBuilder,而不是String连接。

import java.util.*;

public class ans{
    public static void main(String[] args){
    Bears bears = new Bears();
    bears.add(new Bear());
    bears.add(new Bear());
    bears.add(new Bear());

    System.out.println(bears);
    }
}

class Bear{
    public String toString(){
    return "I am a bear";
    }
}

class Bears{
    private ArrayList<Bear> bears = new ArrayList<Bear>();

    public void add(Bear bear){
    bears.add(bear);
    }

    public String toString(){
    StringBuilder str = new StringBuilder();
    if(!bears.isEmpty()){ // If there is no bears, return empty string
        str.append(bears.get(0)); // Append the first one
        for(int index = 1; index < bears.size(); index++){ // For all others
        str.append(" - "); // Append a separator and the bear string
        str.append(bears.get(index));
        }

    }
    return str.toString();
    }
}

编辑要打印A-B-C-D,只需将每个项目与除一个之外的分隔符相关联。 A(-B)( - C)( - D)或(A - )(B - )(C-)D。您可以轻松添加开头和结尾标记。

答案 2 :(得分:0)

如果您需要打印出整个Bears系列,您需要给Bear一个toString,类似于:

return "Bear("+name+","+points+","+race+")";

然后,在BearCollection的toString中,只需为toString中的每个循环编写一个,然后在集合中的每个熊上调用toString,打印出来。