-5

I have been working on a toString method printing out the contents of an ArrayList. I was just wondering if someone could explain to me on how to break each individual Person, Age and DOB on a new line (\n) instead of having them print in one long array with brackets.

Here is the output for my code as well as the expected output.

expected Name: \n"Peter", Age: 16, DOB: Feb 15, 2000\n"Colin", Age: 15, DOB: March 12, 2001  
actual   Name: \n["Peter", Age: 16, DOB: Feb 15, 2000, "Colin", Age: 15, DOB: March 12, 2001]
4
  • I think this question has been answered already. Try looking here: stackoverflow.com/questions/2047003/print-arraylist-element Commented Apr 13, 2016 at 1:06
  • What, you just want us to code this for you? Commented Apr 13, 2016 at 1:06
  • @ScaryWombat clearly if I have some actual output and some expected output I must have written the code, I was not asking someone to code this for me, merely asking how to split it up. Commented Apr 13, 2016 at 1:35
  • @GunnerStone thank you I shall have a look through and hopefully it explains how it is done. Commented Apr 13, 2016 at 1:36

1 Answer 1

0

The simplest way (Java8) would be:

System.out.println(things.stream()
                         .map(Object::toString)
                         .collect(Collectors.joining("\n")));

But I'm guessing this is homework, so using Java8, lambdas, streams, map, and collector would all be off the table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.