简体   繁体   English

在toString中打印一个Arraylist,并用行分隔每个条目

[英]Printing an Arraylist in toString with lines separating each entry

I have a toString that needs to print a lot of stuff, including an Arraylist which contains multiple entries. 我有一个toString,需要打印很多东西,包括一个包含多个条目的Arraylist。 Those entries have to be separated by a new line. 这些条目必须用新行分隔。 Here is the toString code that I am working with right now: 这是我现在正在使用的toString代码:

@Override
public String toString() // Displays the info for a class
    {            
        return getCourseId() + "\n" + getCourseName() + "\n" + getCourseCode()
            + "\n" + "\n" + "Instructor" + "\n" + "-------------------------" 
            + "\n" + Instructor.toString() + "\n" + "\n" + "Student Roster" 
            + "\n" + "-------------------------" + "\n" + roster;
    }

The roster does print, but all of the entries exist on the same line with brackets and commas. 名册确实会打印,但是所有条目都在同一行中,并带有方括号和逗号。

My instructor insists that the toString be self-contained, so everything that I have in the toString currently has to stay there. 我的老师坚持说toString必须是独立的,所以我在toString中拥有的所有内容目前都必须呆在那里。

The roster prints like this: 名册打印如下:

@Override
public String toString() // Displays the info for a person in order
    {
        return getPersonId() + "\t" + getLastName() + "\t" + getFirstName()
            + "\t" + getMajor() + "\t" + getGpa();
    }

Currently, I get the output that looks like this: 当前,我得到的输出看起来像这样:

10000
College Algebra
MATH 101

Instructor
-------------------------
X00009876   Jones   Jane    Associate Professor Mathematics

Student Roster
-------------------------
[X00000002  Smith   Sally   History         2.98, X00000003 Adams   Amanda     Civil Engineering    3.7, X00000005  Turner  Thomas  Nursing         2.34]

But I would like it to look like this: 但我希望它看起来像这样:

10000
College Algebra
MATH 101

Instructor
-------------------------
X00009876   Jones   Jane    Associate Professor Mathematics

Student Roster
-------------------------
X00000002   Smith   Sally   History         2.98
X00000003   Adams   Amanda     Civil Engineering    3.7
X00000005   Turner  Thomas  Nursing         2.34

Any suggestions would be greatly appreciated. 任何建议将不胜感激。 Thanks! 谢谢!

Try this: 尝试这个:

String rosterStr = roster.stream()
        .map(r -> r.toString())
        .collect(Collectors.joining("\n"))

Which will get the string for each value in roster, and then join them with newlines 它将获取花名册中每个值的字符串,然后将它们与换行符连接

I'm not sure exactly what you're asking, but I would suggest using the StringBuilder class, as follows: 我不确定您要问的是什么,但是我建议使用StringBuilder类,如下所示:

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();

    //You haven't provided much info about your ArrayList, customise this accordingly
    for (Object o : ArrayList<Object>) {
        sb.append(o.toString() + " ");
    }

    sb.setLength(sb.length() - 1);

    return getCourseId() + "\n" + getCourseName() + "\n" + getCourseCode()
        + "\n" + "\n" + "Instructor" + "\n" + "-------------------------"
        + "\n" + Instructor.toString() + "\n" + "\n" + "Student Roster"
        + "\n" + "-------------------------" + "\n" + sb.toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM