• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print out all the elements inside the ArrayList  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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<>();??

Here is my source code to help you



My main class AddressBook





My Contact class











 
Carl McGraw
Greenhorn
Posts: 7
1
Linux Python VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, please BeForthrightWhenCrossPostingToOtherSites ,- that;s a link; click it
http://www.java-forums.org/new-java/93128-how-print-stored-content-when-using-arraylist.html
 
Silviu Burcea
Ranch Hand
Posts: 61
1
IntelliJ IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!