简体   繁体   中英

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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