【问题标题】:Class toString() method that returns a formatted array of objects返回格式化对象数组的类 toString() 方法
【发布时间】:2023-03-21 19:20:01
【问题描述】:

我的情况是我必须为一个类创建一个 toString() 方法,该方法基本上只是从另一个类中获取一堆对象并将所有对象放入一个数组列表中。制作 arraylist 的类需要这个 toString() 方法以格式化字符串返回 arraylist 的所有对象,该字符串将准备在控制台中显示。创建放入 arraylists 的对象的类有一个自己的 toString() 方法,它返回所有数据的格式良好的字符串,也许我可以为另一个类的每个对象调用这个 toString() 方法 toString( ) 方法?代码如下:

创建基础对象的类:

public class Batter {

    private String firstName;
    private String lastName;
    private int atBats;
    private int hits;
    private int doubles;
    private int triples;
    private int homeRuns;

    public Batter(String fName, String lName){
        firstName = fName;
        lastName = lName;
    }
    public void setBats(int batCount){
        atBats = batCount;
    }
    public void setHits(int hitCount){
        hits = hitCount;
    }
    public void setDoubles(int doubleCount){
        doubles = doubleCount;
    }
    public void setTriples(int tripleCount){
        triples = tripleCount;
    }
    public void setHR(int homeRunCount){
        homeRuns = homeRunCount;
    }

    public double getAve(int hits, int bats){
        double average = hits / bats;
        return average;
    }
    public String getName(){
        String fullName = this.lastName + "," + this.firstName;
        return fullName;
    }
    public boolean equals(){

        return true;
    }
    public String toString(){
        return "Player: "+getName()+"\nAt Bats: "+this.atBats+"\nHits: "+this.hits+"\nDoubles: "+this.doubles+"\nTriples: "+this.triples+"\nHome Runs: "+this.homeRuns;
    }
}

将由上述类创建的对象放入数组列表的类 - 这是我不知道该怎么做的地方。我需要编写的代码将在该类的 toString() 方法中。同样,它的目标是以格式良好的字符串返回所有对象的数据,可能以某种方式使用在其他类中创建的对象的 toString() 方法:

public class BatterDB
{
    private ArrayList<Batter> theBatters = new ArrayList<Batter>(); // ArrayList of Batters
    private int num;                // int to store logical size of DB

    // Initialize this BatterDB
    public BatterDB()
    {
        num = 0;
    }


    public void addBatter(Batter b)
    {
        theBatters.add(b);
        num++;
    }


    public Batter removeBatter(Batter b)
    {
        return theBatters.remove(0);
    }

    // Return logical size of the DB
    public int numBatters()
    {
        return theBatters.size();
    }


    public Batter findBatter(String fName, String lName)
    {
        return theBatters.get(0); //change
    }


    public void sortName()
    {
    }


    public void sortAve()
    {
    }


    public String toString()
    {
        //Where I'm lost
    }


    public String toStringFile()
    {
        return "toStringFile()";
    }

}

【问题讨论】:

  • 您想遍历thebatters 的列表,然后将它们连接成一个大的String。正确吗?
  • theBatters.stream().map(b -&gt; b.toString()).collect(Collectors.joining(", "))

标签: java arraylist tostring


【解决方案1】:

您要做的是将List theBatters 变成String。在BatterDBtoString 方法中,执行以下操作:

return theBatters.stream()    // get all of them in a stream
    .map(b -> b.toString())    // turn them into strings
    .collect(Collectors.joining(", "))    // join them with commas
                                          // (also can be anything, like "\n" for new lines)

【讨论】:

  • 当当!我以前从未在java中见过这个,很酷,谢谢!!!
  • 它是 Java 8 Streams API 的一部分(这使得事情如此如此变得更加容易)
猜你喜欢
  • 2015-12-12
  • 1970-01-01
  • 2019-04-21
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 2013-02-16
  • 1970-01-01
相关资源
最近更新 更多