Printing an Arraylist in toString with lines separating each entry

放肆的年华 提交于 2019-12-23 05:44:15

问题


I have a toString that needs to print a lot of stuff, including an Arraylist which contains multiple entries. Those entries have to be separated by a new line. Here is the toString code that I am working with right now:

@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.

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!


回答1:


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




回答2:


I'm not sure exactly what you're asking, but I would suggest using the StringBuilder class, as follows:

@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();
}


来源:https://stackoverflow.com/questions/36625109/printing-an-arraylist-in-tostring-with-lines-separating-each-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!