Reputation: 39
I'm simulating a PhoneBook where i have an ArrayList<Contact>
. How to overide the toString()
function in order to have some thing like this wen doing System.out.println(phonebook)
?
Name: name1; Number: number1;
Name: name2; Number: number2;
Name: name3; Number: number3;
etc.....
This worked:
public String toString() {
String phoneBookString = "";
for(Contact contact : list) { phoneBookString += contact.toString() + "\n"; }
return phoneBookString; }
Upvotes: 0
Views: 576
Reputation: 83
First, there is no nice way to @Override the toString() method of ArrayList. but there are other nice options:
Suggestion number one: Override the toString() method of the object the in your list. ArrayList automatically invokes the toString of the enclosing types to print string representation of elements. then you can do this:
// Converting ArrayList to String using Spring API , result is comma separated String
String phonebookToString = StringUtils.collectionToCommaDelimitedString(phonebook);
System.out.println("String converted from ArrayList : " + phonebookToString);
or this:
String phonebookString = "";
for (Phone p : phonebook)
{
phonebookString += p.toString() + "\t";
}
System.out.println(phonebookString);
Suggestion number two: Write static method
static void phoneBookToString(ArrayList<Phone> phonebook) { ... }
that do what i write in suggestion number one.
Upvotes: 0
Reputation: 31689
If you really want to have toString
of an ArrayList
behave differently then the ArrayList
's built-in toString
, you can define your own subclass:
class ContactList extends ArrayList<Contact> {
// duplicate the constructors in ArrayList
public ContactList() { super(); }
public ContactList(Collection<? extends E> c) { super(c); }
public ContactList(int initialCapacity) { super(initialCapacity); }
@Override
public String toString() {
// your version
}
}
Now you can create objects of type ContactList
, and they will behave just like an ArrayList<Contact>
except that toString()
will behave differently.
However, if your plan is for this to be used only inside some other PhoneBook
class, then it's not worth the effort; best to write a private method inside PhoneBook
:
private String toString(ArrayList<Contact> a) { ... }
and use that instead. (That would be the case if you have a PhoneBook class that has a private ArrayList<Contact>
member, and your only use of this is to write the toString()
of your PhoneBook
class. There's no real need to write an overriding toString()
for the ArrayList<Contact>
member; just write a utility method to create the string you want.)
Upvotes: 0
Reputation: 69259
You will need to override the toString()
method of Contact
first:
@Override
public String toString() {
return "your string representation";
}
And then when printing out this code, your phonebook
, you want to do do a for
-loop:
for (Contact contact : phonebook) {
System.out.println(contact);
}
It might be worth considering making Phonebook
an own class, such that you could implement its own toString()
method.
An extra, when using Java 8, you could do the following instead of writing 3 lines of code you could write it in one longer line:
phonebook.forEach(System.out::println);
This will call System.out.println(T)
, resolved to System.out.println(Phonebook)
, for all your Contact
instances in phonebook
.
Upvotes: 1
Reputation: 2826
You can define your own toString()
method for a single object using anonymous class like this:
ArrayList<Contact> phonebook = new ArrayList<Contact>() {
@Override
public String toString() {
// do stuff
}
};
Upvotes: 3