How to print out all the elements inside the ArrayList

Aron Silvester

Ranch Hand

Posts: 63

posted 3 years ago

When this program runs it gives me

workbook.Contact@46e5590e
Number of contact: 1

The number of Contact that I input is just one when I run the program so the Number of contacts: 1 is correct, but it gives me workbook.Contact@46e5590e instead of printing out all the contacts stored inside the Contact class. Yes I do loop through the ArrayList and I also have a method inside the Contact class, the printNameAndPhone(), which prints out the name as well as the phone number but how do I incorporate the printNameAndPhone() method (located in the Contact class) inside the print() method (located inside the AddressBook class)???

Basically I'm asking how to access all the elements in the ArrayList<Contact> addressBook = new ArrayList<>();??

First a comment: Be careful adding "print" statements inside of classes methods. They tend not to be exactly what you want. You may come back and use the class later and be very confused at why it is printing.

There is a better solution though and it has to do with why you're seeing the strange message when printing your class.

The reason you're seeing this workbook.Contact@46e5590e is because Java interprets any custom class made into a String as a <class name>@<memory address> type of format. You can override this basic behavior relatively easily in

To do this simply override (create) the toString() method in your class. Java will automatically call this when printing or combining with other strings.

Everything that follows from this is going to assume that you've created the toString() method as I did above...

Then when you want to print out the contact you can simple do this:

This will print out the expected contact.

It is also helpful to do this because you might want to combine it with other strings/formats:

Conrado Sanchez wrote:The number of Contact that I input is just one when I run the program so the Number of contacts: 1 is correct, but it gives me workbook.Contact@46e5590e instead of printing out all the contacts stored inside the Contact class. Yes I do loop through the ArrayList and I also have a method inside the Contact class, the printNameAndPhone(), which prints out the name as well as the phone number but how do I incorporate the printNameAndPhone() method (located in the Contact class) inside the print() method (located inside the AddressBook class)???

Basically I'm asking how to access all the elements in the ArrayList<Contact> addressBook = new ArrayList<>();??

So you can use my above method, It will print out the addressBook as a list of contacts:

This is currently what you're doing. If you'd like to control how it prints more you could simple loop through the addressBook (since its a list after all)

But once again there is a better solution here I think. But this requires a bit more work on your overall program design. Which would be more in depth and may be overkill for a simple problem (if you're just trying to solve a simple problem and not trying to design an actual system).

Aron Silvester

Ranch Hand

Posts: 63

posted 3 years ago

Thank you that last bit really helped me to control how the elements get printed out. One more question. I still need the find method where the application prompts the user for the text it will use to search the address book. After the user enters the text, the application displays a list of all contacts that contain that text in either the contact's name or phone number. So lets say the user enters in 808, the application searches the ArrayList for all the contacts that contains 808 in them. How would I go about accessing specific elements in the ArrayList? This would require to loop through the ArrayList again and compare the text input of the user with the elements of ArrayList. I have no idea as to how to start this.

Conrado Sanchez wrote:Thank you that last bit really helped me to control how the elements get printed out. One more question. I still need the find method where the application prompts the user for the text it will use to search the address book. After the user enters the text, the application displays a list of all contacts that contain that text in either the contact's name or phone number. So lets say the user enters in 808, the application searches the ArrayList for all the contacts that contains 808 in them. How would I go about accessing specific elements in the ArrayList? This would require to loop through the ArrayList again and compare the text input of the user with the elements of ArrayList. I have no idea as to how to start this.

If you need lookup, you should use a Map instead of ArrayList, or even better(if you start the search at the beginning of the number), a Trie structure. You are looking for String methods like contains