import java.util.*; public class Person { private String name; private ArrayList friends; public Person(String name){ this.name = name; friends = new ArrayList(); } public void addFriend(Person p){ friends.add(p); } public ArrayList getFriends(){ return friends; } public String getName(){ return name; } public String toString(){ StringBuilder s = new StringBuilder (""); s.append(name).append(" :"); for (Person p : friends){ s.append(" ").append(p.getName()); } return s.toString(); } // // method rewritten by Kerry Johnstone, to use StringBuilder // This is more efficient. Here are Kerry's comments (13/01/2017) /* I did a little digging, and found out that StringBuilder creates and manipulates an array of characters so that whenever you insert and amend new pieces into your StringBuilder object it's adding new elements to the array. Then once you've finished manipulating your StringBuilder, calling it's toString() method then converts this array of characters into a String. I did see some conflicting information on the exact implementation of the array itself, with one person on stack overflow suggesting the latest version of StringBuilder uses linked lists. Either way it's very interesting. */ }